I have a following dataframe:
df1 <- structure(list(name = c("ene", "due", "rabe", "rabe", "kum",
"kum", "kum", "rike", "smake"), type = c("a", "b", "d", "a",
"c", "c", "b", "d", "a")), class = "data.frame", row.names = c(NA,
-9L))
And I would like to transform it to the following dataframe:
df2 <- structure(list(name = c("ene", "due", "rabe", "kum", "rike",
"smake"), type_a = c(1, 0, 1, 0, 0, 1), type_b = c(0, 1, 0, 1,
0, 0), type_c = c(0, 0, 0, 2, 0, 0), type_d = c(0, 0, 1, 0, 1,
0)), class = "data.frame", row.names = c(NA, -6L))
Basically I want to split "type" column for as many columns as categories stored with the original one. Also, instead of character values I would like to count the occurences of each category per name.
How to do it in R?
EDIT:I tried to do so with spread from tidyr, but it throws an error due to non-unique combination of keys.