9.5 Long format data
- Longformat: Observations of same units are stacked on top of each other
- Row = unit \(\times\) time = observations
- Columns: unit id, time and variables that are spread out (rarely variables are also stacked)
- Table 9.2 below is “real” longformat accepted by most stat software
- Use
tidyr
package (alternativesreshape
package)pivot_longer()
to collect variables into one column,separate
to generate two columns for variables and time (here separator is a “.”) andpivot_wider()
to spread variables across columns keeping time column- To my knowledge
pivot_longer()
,pivot_wider()
were introduced after neg. community feedback to replacegather()
andspread()
unit | time | trust | Victimization |
---|---|---|---|
Paul | 2006 | 5 | 0 |
Paul | 2007 | 5 | 1 |
Pedro | 2006 | 6 | 1 |
Pedro | 2007 | 6 | 0 |
Peter | 2006 | 1 | 0 |
Peter | 2007 | 1 | 1 |