[R] ordering and factors into column headings

Gabor Grothendieck ggrothendieck at gmail.com
Wed Sep 2 02:14:04 CEST 2009


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.
>
>




More information about the R-help mailing list