[R] Creating panel data

David Winsemius dwinsemius at comcast.net
Tue Jul 24 01:33:58 CEST 2012


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

> At 10:38 AM 7/23/2012, you wrote:
>> You might also find it useful to use Hadley Wickham's plyr
>> and/or reshape2 packages, whose aim is to standardize and simplify
>> data manipulation tasks.
>>
>> Cheers,
>> Bert
>
>
> I have already used R enough to have correctly imported the actual  
> data. After import, it is in the approximate format at the x  
> dataframe I previously posted. I already found the plyr and reshape2  
> packages and had assumed that the cast (or dcast) options might be  
> the correct ones. Melt seemed to get me only what I already have.  
> The examples I have seen thus far start with data in a various  
> formats and end up in the format that I am starting with. In other  
> words, they seem to do the exact opposite of what I'm trying to do.  
> So I'm still stuck with how to get started and whether the functions  
> in reshape2 are actually the correct ones to consider.
>
> ...still looking for some help on this.
>

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

-- 

David Winsemius, MD
Alameda, CA



More information about the R-help mailing list