Dear R-help list, Apologies. I am trying to convert one table to another. It feels that it should be a very straightforward answer with a single (or two) commands with the right extensions, but I really can't figure this out right now. I have several hundred pheno factors actually, so manually doing this line by line is not an option. My original table is approximately like this : ID pheno 1 A Breast Cancer 2 A Appendicitis 3 A Microcephaly 4 B Polyps 5 B Autism 6 C Autism 7 D Breast Cancer 8 D Polyps 9 D Appendicitis 10 E Breast Cancer What I want is this : ID Breast Cancer Appendicitis Microcephaly Polyps Autism A 1 1 1 B 1 1 C 1 D 1 1 E 1 The data is here data <- data.frame(ID=c(rep("A",3),rep("B",2),rep("C",1),rep("D",3),rep("E",1)), pheno=c("Breast Cancer","Appendicitis","Microcephaly","Polyps","Autism","Autism","Breast Cancer","Polyps","Appendicitis","Breast Cancer")) Thank you very much! Many apologies. Min-Han [[alternative HTML version deleted]]
Joachim de Lezardiere
2010-May-06 19:38 UTC
[R] Apologies : question on transforming a table
Can you explain more about the output you want ? Thank you -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Min-Han Tan Sent: Thursday, May 06, 2010 2:28 PM To: r-help at stat.math.ethz.ch Subject: [R] Apologies : question on transforming a table Dear R-help list, Apologies. I am trying to convert one table to another. It feels that it should be a very straightforward answer with a single (or two) commands with the right extensions, but I really can't figure this out right now. I have several hundred pheno factors actually, so manually doing this line by line is not an option. My original table is approximately like this : ID pheno 1 A Breast Cancer 2 A Appendicitis 3 A Microcephaly 4 B Polyps 5 B Autism 6 C Autism 7 D Breast Cancer 8 D Polyps 9 D Appendicitis 10 E Breast Cancer What I want is this : ID Breast Cancer Appendicitis Microcephaly Polyps Autism A 1 1 1 B 1 1 C 1 D 1 1 E 1 The data is here data <- data.frame(ID=c(rep("A",3),rep("B",2),rep("C",1),rep("D",3),rep("E",1)), pheno=c("Breast Cancer","Appendicitis","Microcephaly","Polyps","Autism","Autism","Breast Cancer","Polyps","Appendicitis","Breast Cancer")) Thank you very much! Many apologies. Min-Han [[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.
On May 6, 2010, at 3:27 PM, Min-Han Tan wrote:> Dear R-help list, > > Apologies. I am trying to convert one table to another. It feels > that it > should be a very straightforward answer with a single (or two) > commands with > the right extensions, but I really can't figure this out right now. > I have > several hundred pheno factors actually, so manually doing this line > by line > is not an option. > > My original table is approximately like this : > > ID pheno > 1 A Breast Cancer > 2 A Appendicitis > 3 A Microcephaly > 4 B Polyps > 5 B Autism > 6 C Autism > 7 D Breast Cancer > 8 D Polyps > 9 D Appendicitis > 10 E Breast Cancer > > > What I want is this : > ID Breast Cancer Appendicitis Microcephaly Polyps Autism A 1 1 > 1 B 1 > 1 C 1 D 1 1 E 1 >On the off-chance, as it were, that you really want what comes across after html posting... c("ID", data$pheno, unlist(sapply(1:length(tid), function(x) c(names(tid[x]), rep(1, tid[[x]])))) ) [1] "ID" "Breast\nCancer" "Appendicitis" "Microcephaly" [5] "Polyps" "Autism" "Autism" "Breast \nCancer" [9] "Polyps" "Appendicitis" "Breast Cancer" "A" [13] "1" "1" "1" "B" [17] "1" "1" "C" "1" [21] "D" "1" "1" "1" [25] "E" "1" >> The data is here > data <- > data.frame(ID=c(rep("A",3),rep("B",2),rep("C",1),rep("D",3),rep("E", > 1)), > pheno=c("Breast > Cancer > ","Appendicitis","Microcephaly","Polyps","Autism","Autism","Breast > Cancer","Polyps","Appendicitis","Breast Cancer")) > > Thank you very much! Many apologies. > > Min-Han > > [[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.David Winsemius, MD West Hartford, CT
This is definitely a hack, but it gets the job done. X<-model.matrix(~0+pheno,data=data) data2<-apply(X,2,function(X){tapply(X,data$ID,sum)}) data2 phenoAppendicitis phenoAutism phenoBreast Cancer phenoMicrocephaly phenoPolyps A 1 0 1 1 0 B 0 1 0 0 1 C 0 1 0 0 0 D 1 0 1 0 1 E 0 0 1 0 0 -tgs On Thu, May 6, 2010 at 3:27 PM, Min-Han Tan <minhan.science@gmail.com>wrote:> Dear R-help list, > > Apologies. I am trying to convert one table to another. It feels that it > should be a very straightforward answer with a single (or two) commands > with > the right extensions, but I really can't figure this out right now. I have > several hundred pheno factors actually, so manually doing this line by line > is not an option. > > My original table is approximately like this : > > ID pheno > 1 A Breast Cancer > 2 A Appendicitis > 3 A Microcephaly > 4 B Polyps > 5 B Autism > 6 C Autism > 7 D Breast Cancer > 8 D Polyps > 9 D Appendicitis > 10 E Breast Cancer > > > What I want is this : > ID Breast Cancer Appendicitis Microcephaly Polyps Autism A 1 1 1 B 1 > 1 C 1 D 1 1 E 1 > > The data is here > data <- > data.frame(ID=c(rep("A",3),rep("B",2),rep("C",1),rep("D",3),rep("E",1)), > pheno=c("Breast > Cancer","Appendicitis","Microcephaly","Polyps","Autism","Autism","Breast > Cancer","Polyps","Appendicitis","Breast Cancer")) > > Thank you very much! Many apologies. > > Min-Han > > [[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. >[[alternative HTML version deleted]]
Dear Bill - Thank you very much! This works perfectly! Apologies to all those who could not visualize the formatting of the request, I should default to plain text mail. Thank you all once again, once again, I am very grateful to the R-help forum for being just wonderful! Min-Han On Thu, May 6, 2010 at 3:38 PM, William Dunlap <wdunlap@tibco.com> wrote:> > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > > > -----Original Message----- > > From: r-help-bounces@r-project.org > > [mailto:r-help-bounces@r-project.org] On Behalf Of Min-Han Tan > > Sent: Thursday, May 06, 2010 12:28 PM > > To: r-help@stat.math.ethz.ch > > Subject: [R] Apologies : question on transforming a table > > > > Dear R-help list, > > > > Apologies. I am trying to convert one table to another. It > > feels that it > > should be a very straightforward answer with a single (or > > two) commands with > > the right extensions, but I really can't figure this out > > right now. I have > > several hundred pheno factors actually, so manually doing > > this line by line > > is not an option. > > > > My original table is approximately like this : > > > > ID pheno > > 1 A Breast Cancer > > 2 A Appendicitis > > 3 A Microcephaly > > 4 B Polyps > > 5 B Autism > > 6 C Autism > > 7 D Breast Cancer > > 8 D Polyps > > 9 D Appendicitis > > 10 E Breast Cancer > > > > > > What I want is this : > > ID Breast Cancer Appendicitis Microcephaly Polyps Autism > > A 1 1 1 B 1 > > 1 C 1 D 1 1 E 1 > > My mailer messed up your display, but does > table() work for you? E.g., > > with(data, table(ID, pheno)) > pheno > ID Appendicitis Autism Breast Cancer Microcephaly Polyps > A 1 0 1 1 0 > B 0 1 0 0 1 > C 0 1 0 0 0 > D 1 0 1 0 1 > E 0 0 1 0 0 > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > > > > > The data is here > > data <- > > data.frame(ID=c(rep("A",3),rep("B",2),rep("C",1),rep("D",3),re > > p("E",1)), > > pheno=c("Breast > > Cancer","Appendicitis","Microcephaly","Polyps","Autism","Autis > > m","Breast > > Cancer","Polyps","Appendicitis","Breast Cancer")) > > > > Thank you very much! Many apologies. > > > > Min-Han > > > > [[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. > > >[[alternative HTML version deleted]]
Henrique Dallazuanna
2010-May-07 02:07 UTC
[R] Apologies : question on transforming a table
Try dhis: xtabs(~ ID + pheno, data = data) On Thu, May 6, 2010 at 4:27 PM, Min-Han Tan <minhan.science@gmail.com>wrote:> Dear R-help list, > > Apologies. I am trying to convert one table to another. It feels that it > should be a very straightforward answer with a single (or two) commands > with > the right extensions, but I really can't figure this out right now. I have > several hundred pheno factors actually, so manually doing this line by line > is not an option. > > My original table is approximately like this : > > ID pheno > 1 A Breast Cancer > 2 A Appendicitis > 3 A Microcephaly > 4 B Polyps > 5 B Autism > 6 C Autism > 7 D Breast Cancer > 8 D Polyps > 9 D Appendicitis > 10 E Breast Cancer > > > What I want is this : > ID Breast Cancer Appendicitis Microcephaly Polyps Autism A 1 1 1 B 1 > 1 C 1 D 1 1 E 1 > > The data is here > data <- > data.frame(ID=c(rep("A",3),rep("B",2),rep("C",1),rep("D",3),rep("E",1)), > pheno=c("Breast > Cancer","Appendicitis","Microcephaly","Polyps","Autism","Autism","Breast > Cancer","Polyps","Appendicitis","Breast Cancer")) > > Thank you very much! Many apologies. > > Min-Han > > [[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]]