Reputation: 113
I would like to take date input like this:
library(plyr)
names(ChickWeight) <- tolower(names(ChickWeight))
ChickWeight$diet.name <- as.factor(revalue(ChickWeight$diet, c("1" = "Grain", "2" = "Grubs", "3" = "Scraps", "4" = "Free Range")))
which creates:
> head(ChickWeight)
weight time chick diet diet.name
1 42 0 1 1 Grain
2 51 2 1 1 Grain
3 59 4 1 1 Grain
4 64 6 1 1 Grain
5 76 8 1 1 Grain
6 93 10 1 1 Grain
> tail(ChickWeight)
weight time chick diet diet.name
573 155 12 50 4 Free Range
574 175 14 50 4 Free Range
575 205 16 50 4 Free Range
576 234 18 50 4 Free Range
577 264 20 50 4 Free Range
578 264 21 50 4 Free Range
And from those data, extract another set of data (chick.feed
) that looks like this:
chick diet.name
1 Grain
2 Grain
. .
. .
. .
21 Grubs
22 Grubs
. .
. .
. .
31 Scraps
32 Scraps
. .
. .
. .
49 Free Range
50 Free Range
I.e., I would like to create a "pivot table" (chick.feed
) in which the computed value for each row (here, each chick
) is the alphabetic value ("factor" in R-speak) of all the various rows for that chick in the ChickWeight
.
(My question appears to be similar but not identical to this one about converting from long to wide data.)
Upvotes: 0
Views: 142
Reputation: 6073
No need to reshape. Just grab the desired columns and keep the unique rows:
unique(ChickWeight[ , c("chick", "diet.name")])
Upvotes: 1