Skip to content
Prev 300992 / 398503 Next

Creating panel data

On Jul 23, 2012, at 10:33 AM, Jeff wrote:

            
I didn't see a clear way to use either reshape() or the plyr/reshape2  
packages to do this, (but would enjoy seeing an example that improved  
my understanding on this path)  so I just looked at your "x" and then  
created a scaffold with the number of rows needed to match your "y"  
and filled in the the other columns by first merging to that scaffold  
and then creating new columns:

 > y2 <- data.frame(id=rep(1:2, each=5), Year=seq(68,69,by=0.25) )
 > merge(y2, x)
    id  Year Event1Occur YearOfOccurEvent1 Event2Occur YearOfOccurEvent2
1   1 68.00           1             68.25           0               0.0
2   1 68.25           1             68.25           0               0.0
3   1 68.50           1             68.25           0               0.0
4   1 68.75           1             68.25           0               0.0
5   1 69.00           1             68.25           0               0.0
6   2 68.00           0              0.00           1              68.5
7   2 68.25           0              0.00           1              68.5
8   2 68.50           0              0.00           1              68.5
9   2 68.75           0              0.00           1              68.5
10  2 69.00           0              0.00           1              68.5
 > y2a <- merge(y2, x)

 > y2a$Event1 <- with( y2a, as.numeric( Event1Occur & Year>=  
YearOfOccurEvent1) )
 > y2a$Event2 <- with( y2a, as.numeric( Event2Occur & Year>=  
YearOfOccurEvent2) )

# Using negative numeric column indexing to suppress then now  
superfluous columns

 > y2a[, -(3:6) ]
    id  Year Event1 Event2
1   1 68.00      0      0
2   1 68.25      1      0
3   1 68.50      1      0
4   1 68.75      1      0
5   1 69.00      1      0
6   2 68.00      0      0
7   2 68.25      0      0
8   2 68.50      0      1
9   2 68.75      0      1
10  2 69.00      0      1