This question already has an answer here:
Have a data frame like this
other=data.frame(name=c("a","b","a","c","d"),result=c("Y","N","Y","Y","N"))
How can I use spread function in tidyr or other function to get the count of result Y or N as column header like this
name Y N
a 2 0
b 0 1
Thanks
These are a few ways of many to go about it:
1) With library
dplyr
, you can simply group things and count into the format needed:2) You can use a combination of
table
andtidyr
spread as follows:3) You can use a combination of
dplyr
andtidyr
to do as follows:Here is another option using
dcast
fromdata.table
Although,
table(other)
would be a compact option (from @mtoto's comments), for large datasets, it may be more efficient to usedcast
. Some benchmarks are given below