[R] reshape dataframe

jim holtman jholtman at gmail.com
Fri Mar 20 17:13:50 CET 2009


Does this do what you want:

>  x<-data.frame(id=c(1,2,3), snp1=c("AA","GG",
+   "AG"),snp2=c("GG","AG","GG"),snp3=c("GG","AG","AA"))
> do.call(rbind, apply(x, 1, function(.row){
+     data.frame(c(.row[1], .row[1]),
+                strsplit(.row[2], ''),
+                strsplit(.row[3], ''),
+                strsplit(.row[4], ''))
+ }))
  c..row.1....row.1.. snp1 snp2 snp3
1                   1    A    G    G
2                   1    A    G    G
3                   2    G    A    A
4                   2    G    G    G
5                   3    A    G    A
6                   3    G    G    A


On Fri, Mar 20, 2009 at 10:29 AM, Duijvesteijn, Naomi
<Naomi.Duijvesteijn at ipg.nl> wrote:
>
>   Hi,
>
>
>   I have a large dataset on which I would like to do the following:
>
>
>   x<-data.frame(id=c(1,2,3), snp1=c("AA","GG",
>   "AG"),snp2=c("GG","AG","GG"),snp3=c("GG","AG","AA"))
>
>   > x
>
>     id snp1 snp2 snp3
>
>   1  1   AA   GG   GG
>
>   2  2   GG   AG   AG
>
>   3  3   AG   GG   AA
>
>
>   And  then reshape the dataset in such a way that the individuals get 2
>   observations for every snp by splitting the genotypes (in this case):
>
>
>   id snp1 snp2 snp3
>
>   1 A G A
>
>   1 A G G
>
>   2 G A G
>
>   2 G G G
>
>   3 G A A
>
>   3 G G A
>
>
>   It is probably quite easy to solve, but I can’t get it right…
>
>
>   Thanks for your help and valuable time!
>
>
>   Naomi Duijvesteijn
>
>
>
>
>
>
>   Disclaimer:  De  informatie opgenomen in dit bericht (en bijlagen) kan
>   vertrouwelijk zijn en is uitsluitend bestemd voor de geadresseerde(n).
>   Indien u dit bericht ten onrechte ontvangt, wordt u geacht de inhoud niet te
>   gebruiken, de afzender direct te informeren en het bericht te vernietigen.
>   Aan dit bericht kunnen geen rechten of plichten worden ontleend.
>
>   ----------------------------------------------------------------------------
>   ----------------------------
>
>   Disclaimer: The information contained in this message may be confidential
>   and is intended to be exclusively for the addressee. Should you receive this
>   message unintentionally, you are expected not to use the contents herein, to
>   notify the sender immediately and to destroy the message. No rights can be
>   derived from this message.
>
> ______________________________________________
> 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.
>
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list