[R] Combining Rows from One Data Frame, Outputting into Another

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Sat Aug 2 02:01:21 CEST 2014


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.



More information about the R-help mailing list