Hi, The lab in which I send my samples return the results in a format that is difficult for me to run my analysis. The lab outputs the results where each parameter is its own row and it’s not consistently in the same order (and not each sample is tested for the same suite of variables). e.g.>datasetsite parameter value a e2 3 a e1 1 a e3 5 b e3 1 b e1 2 c e5 4 c e4 5 c e2 2 d e2 4 d e4 3 e e1 2 How would I go about transforming the above table to the one below, where each parameter has its own column and there is only 1 row of data for each site? site e1 e2 e3 e4 e5 a 1 3 5 NA NA b 2 NA 3 NA NA c NA 2 NA 5 4 d NA 4 NA 3 NA e 2 NA NA NA NA I have attempted to write a loop and use the “match” function, but it kept getting more and more complicated. I have a feeling that there is simple solution, but I just don’t see it. Any insight would be helpful. Thanks, Krista [[alternative HTML version deleted]]
Try the reshape package:> library(reshape) > cast(DF, site ~ parameter)site e1 e2 e3 e4 e5 1 a 1 3 5 NA NA 2 b 2 NA 1 NA NA 3 c NA 2 NA 5 4 4 d NA 4 NA 3 NA 5 e 2 NA NA NA NA (or the reshape command in R). On Tue, Sep 1, 2009 at 7:59 PM, Krista Chin<057448c at acadiau.ca> wrote:> Hi, > > The lab in which I send my samples return the results in a format that > is difficult for me to run my analysis. ?The lab outputs the results > where each parameter is its own row and it?s not consistently in the > same order (and not each sample is tested for the same suite of > variables). > > e.g. > >>dataset > > site ? ?parameter ?value > a ? ? ? e2 ? ? ?3 > a ? ? ? e1 ? ? ?1 > a ? ? ? e3 ? ? ?5 > b ? ? ? e3 ? ? ?1 > b ? ? ? e1 ? ? ?2 > c ? ? ? e5 ? ? ?4 > c ? ? ? e4 ? ? ?5 > c ? ? ? e2 ? ? ?2 > d ? ? ? e2 ? ? ?4 > d ? ? ? e4 ? ? ?3 > e ? ? ? e1 ? ? ?2 > > How would I go about transforming the above table to the one below, > where each parameter has its own column and there is only 1 row of data > for each site? > > site ? ?e1 ? ? ?e2 ? ? ?e3 ? ? ?e4 ? ? ?e5 > a ? ? ? 1 ? ? ? 3 ? ? ? 5 ? ? ? NA ? ? ?NA > b ? ? ? 2 ? ? ? NA ? ? ?3 ? ? ? NA ? ? ?NA > c ? ? ? NA ? ? ?2 ? ? ? NA ? ? ?5 ? ? ? 4 > d ? ? ? NA ? ? ?4 ? ? ? NA ? ? ?3 ? ? ? NA > e ? ? ? 2 ? ? ? NA ? ? ?NA ? ? ?NA ? ? ?NA > > I have attempted to write a loop and use the ?match? function, but > it kept getting more and more complicated. ?I have a feeling that there > is simple solution, but I just don?t see it. > > Any insight would be helpful. > > Thanks, > Krista > > ? ? ? ?[[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. > >
Try this also: xtabs(value ~ site + parameter, data = DF) On Tue, Sep 1, 2009 at 8:59 PM, Krista Chin <057448c@acadiau.ca> wrote:> Hi, > > The lab in which I send my samples return the results in a format that > is difficult for me to run my analysis. The lab outputs the results > where each parameter is its own row and it’s not consistently in the > same order (and not each sample is tested for the same suite of > variables). > > e.g. > > >dataset > > site parameter value > a e2 3 > a e1 1 > a e3 5 > b e3 1 > b e1 2 > c e5 4 > c e4 5 > c e2 2 > d e2 4 > d e4 3 > e e1 2 > > How would I go about transforming the above table to the one below, > where each parameter has its own column and there is only 1 row of data > for each site? > > site e1 e2 e3 e4 e5 > a 1 3 5 NA NA > b 2 NA 3 NA NA > c NA 2 NA 5 4 > d NA 4 NA 3 NA > e 2 NA NA NA NA > > I have attempted to write a loop and use the “match” function, but > it kept getting more and more complicated. I have a feeling that there > is simple solution, but I just don’t see it. > > Any insight would be helpful. > > Thanks, > Krista > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@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. > >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]