An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20140801/a0443820/attachment.pl>
Combining Rows from One Data Frame, Outputting into Another
3 messages · Kathy Haapala, Jeff Newmiller, arun
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 next time.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
On August 1, 2014 12:56:21 PM PDT, Kathy Haapala <kathy at haapi.mn.org> wrote:
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_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:
x_new.df
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.
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:
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_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:
x_new.df
? 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.