You could use:
library(dplyr)
library(tidyr)
x.df %>% group_by(Year, Group, Eye_Color) %>% summarize(n=n()) %>%
spread(Eye_Color,n, fill=0)
Source: local data frame [6 x 5]
Year Group blue brown green
1 2000 1 2 1 0
2 2000 2 0 0 2
3 2001 1 1
library(reshape2)
?dcast
Nice example. So nice that it looks like it could be homework... thus the
pointer to docs rather than a full solution. Please read the Posting Guide, and
note that HTML email format is not necessarily a what-you-see-is-what-we-see
format so you should post in plain text
If I have a dataframe x.df as follows:
> x.df <- data.frame(Year = c(2000, 2000, 2000, 2000, 2000, 2001, 2001,
2001, 2001, 2002), Group = c(1, 1, 1, 2, 2, 1, 2, 2, 3, 1), Eye_Color =
c("blue", "blue", "brown", "green", "green", "blue", "brown", "blue",
"blue", "blue"))
> x.df
Year Group Eye_Col
3 matches
Mail list logo