Skip to content
Prev 342778 / 398506 Next

Combining Rows from One Data Frame, Outputting into Another

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???? 0???? 0
4 2001???? 2??? 1???? 1???? 0
5 2001???? 3??? 1???? 0???? 0
6 2002???? 1??? 1???? 0???? 0



Or

library(reshape2)
dcast(x.df, Year+Group~Eye_Color, value.var="Eye_Color")
A.K.
On Friday, August 1, 2014 7:06 PM, Kathy Haapala <kathy at haapi.mn.org> wrote:
If I have a dataframe x.df as follows:
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"))
?  Year Group Eye_Color
1? 2000? ?  1? ? ? blue
2? 2000? ?  1? ? ? blue
3? 2000? ?  1? ?  brown
4? 2000? ?  2? ?  green
5? 2000? ?  2? ?  green
6? 2001? ?  1? ? ? blue
7? 2001? ?  2? ?  brown
8? 2001? ?  2? ? ? blue
9? 2001? ?  3? ? ? blue
10 2002? ?  1? ? ? blue

how can I turn it into a new dataframe that would take the data from
multiple rows of Year/Group combinations and output the data into one row
for each combination, like this:
? Year Group No_blue No_brown No_green
1 2000? ?  1? ? ?  2? ? ? ? 1? ? ? ? 0
2 2000? ?  2? ? ?  0? ? ? ? 0? ? ? ? 2
3 2001? ?  1? ? ?  1? ? ? ? 0? ? ? ? 0
4 2001? ?  2? ? ?  1? ? ? ? 1? ? ? ? 0
5 2001? ?  3? ? ?  1? ? ? ? 0? ? ? ? 0
6 2002? ?  1? ? ?  1? ? ? ? 0? ? ? ? 0

I've been trying to use for loops, but I'm wondering if anyone has a better
or more simple suggestion.

??? [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.