Reputation: 723
My data looks like the following:
fruit ID
apple 1
mango 1
orange 1
grapes 2
strawberries 3
What i want to do using reshape is the foll:
ID Fruit
1 apple,mango,orange
2 grapes
3 strawberries
I tried reshape
rehshape(data=test,direction="long",varying=c(1,2),v.names="v1",timevar="v2",times=c(1,2))->test.long
but that's obviously wrong.
Upvotes: 0
Views: 135
Reputation: 193687
Since everyone else has aggregate
covered, here's a way to make reshape
work for you: Add a time variable to your data.frame
and then try to reshape from long to wide. Assuming your original data.frame
is called "mydf":
> # Create a time variable
> mydf$time <- ave(mydf$ID, mydf$ID, FUN = seq_along)
> mydf
fruit ID time
1 apple 1 1
2 mango 1 2
3 orange 1 3
4 grapes 2 1
5 strawberries 3 1
> # Now, reshape
> reshape(mydf, direction = "wide", idvar = "ID", timevar = "time")
ID fruit.1 fruit.2 fruit.3
1 1 apple mango orange
4 2 grapes <NA> <NA>
5 3 strawberries <NA> <NA>
Upvotes: 2
Reputation: 61214
Try this
> aggregate(fruit~ID, data=DF, FUN=paste0)
ID fruit
1 1 apple, mango, orange
2 2 grapes
3 3 strawberries
Upvotes: 2
Reputation: 17189
You can try aggregate
aggregate(fruit ~ ID, data , paste, sep=',')
ID fruit
1 1 apple, mango, orange
2 2 grapes
3 3 strawberries
Upvotes: 3