A seemingly easy problem is keeping me very busy.
I have a data frame:
> df1
Name Score
1 Ben 1
2 Ben 2
3 John 1
4 John 2
5 John 3
I would like to create a summary of the table like this:
> df2
Name Score_1 Score_2 Score_3
1 Ben 1 1 0
2 John 1 1 1
So df2 must (i) only show unique "Names" and (ii) create columns from the unique factors in "Score" and (iii) count the number of times a person received said score.
I have tried:
df2 <- ddply(df1, c("Name"), summarise
,Score_1 = sum(df1$Score == 1)
,Score_2 = sum(df1$Score == 2)
,Score_3 = sum(df1$Score == 3))
which produces:
Name Score_1 Score_2 Score_3
1 Ben 2 2 1
2 John 2 2 1
So my attempt incorrectly counts all occurences instead of counting "per group"
EDIT:
As per the comments, also tried reshape
(possibly just doing it wrong):
> reshape(df1, idvar = "Name", timevar = "Score", direction = "wide")
Name
1 Ben
3 John
For a start, the "Score" column is missing but worse than that, from my research on reshape
, I am not convinced that I am going to get a count of each factor, which is the whole point.