I am quite new to R and programming in general and have been struggling with the following.
I have a dataframe like below:
id animals
1 cat dog
2 cat pig dog fish fish
3 horse horse
I want to create a new column for each animal containing the frequency count for each id :
id cat dog fish horse pig
1 1 1 0 0 0
2 1 1 2 0 1
3 0 0 0 2 0
How do I achieve this?
example dput:
structure(list(id = 1:3, animals = structure(1:3, .Label = c("cat dog",
"cat pig dog fish fish", "horse horse"), class = "factor")), .Names = c("id",
"animals"), class = "data.frame", row.names = c(NA, -3L))