質問

So I have a data frame with two vectors. Time and team.

df <- data.frame(time=rep(seq(1:3),3), team=LETTERS[rep(1:3,each=3)])

>  time team
>1    1    A
>2    2    A
>3    3    A
>4    1    B
>5    2    B
>6    3    B
>7    1    C
>8    2    C
>9    3    C

How do I split the data.frame by time then merge it back together by time? Something like this.

>  time df.A df.B df.C
>1    1    A    B    C
>2    2    A    B    C
>3    3    A    B    C

I figured out how to split the data.frame using split or dlply but I haven't had any success using a cbind or merge to get the data frame back together.

Also, the lengths of each (split) list are different so any help adding NA into the mix will also be greatly appreciated. Thanks.

役に立ちましたか?

解決

You can use reshape for this:

> df$tmp <- df$team
> reshape(df, idvar='time', timevar='team', direction='wide')
  time tmp.A tmp.B tmp.C
1    1     A     B     C
2    2     A     B     C
3    3     A     B     C
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top