If I have the following data frame called petsin R:
> pets
name animal
1 Amy dog
2 Bob cat
3 Carol dog
4 Danny cat
5 Eustace horse
6 Frances horse
I can unstackset the animal categories as column headers in the data frame as follows:
> unstack(pets, pets$name ~ pets$animal)
cat dog horse
1 Bob Amy Eustace
2 Danny Carol Frances
If, however, the data frame petshas only one instance of each animal:
> pets
name animal
1 Amy dog
2 Bob cat
then executing the same code produces this result:
> unstack(pets, pets$name ~ pets$animal)
res
cat Bob
dog Amy
I need something that puts these animal categories in the column headers, no matter how many rows fall into each category. Anyone have any ideas please?
source
share