Henrik Andersson
2004-Nov-23 15:40 UTC
[R] Create a vector of combinations based on a table column names
I want to create a character vector based on the table (shortened for display) below: Where there are ones in the matrix I want the column name to appear and where there are zeros nothing, which would make the vector in this shortened case: combinations <- ("A B","A C","A E H","A F G","B C D","E G H",A C D E H","A C D F G") no value A B C D E F G H 1 2 3.095 1 1 0 0 0 0 0 0 2 2 1.687 1 0 1 0 0 0 0 0 46 3 3.470 1 0 0 0 1 0 0 1 47 3 1.563 1 0 0 0 0 1 1 0 50 3 6.234 0 1 1 1 0 0 0 0 148 4 3.663 0 0 1 0 1 0 1 1 151 4 3.470 0 0 0 1 1 1 0 1 177 5 5.411 1 0 1 1 1 0 0 1 178 5 6.829 1 0 1 1 0 1 1 0 Question is how to make this not so manually? --------------------------------------------- Henrik Andersson Netherlands Institute of Ecology - Centre for Estuarine and Marine Ecology P.O. Box 140 4400 AC Yerseke Phone: +31 113 577473 h.andersson at nioo.knaw.nl http://www.nioo.knaw.nl/ppages/handersson
Eric Lecoutre
2004-Nov-23 16:12 UTC
[R] Create a vector of combinations based on a table column names
Hi, Here is something that does the job (though I am sure other people will find smarter solutions!): > samp=matrix(sample(0:1,size=100,replace=TRUE ,prob=c(0.8,0.2)),ncol=10) > colnames(samp)<-LETTERS[1:10] > unlist(lapply(apply(samp,1,FUN=function(vec) colnames(samp)[as.logical(vec)] ),paste,collapse=" ")) [1] "A F J" "D I" "B I" "A" "G H" "B C E H" "E H" "B C E G" "E" [10] "B C E I" HTH, Eric At 16:40 23/11/2004, Henrik Andersson wrote:>I want to create a character vector based on the table (shortened for >display) below: >Where there are ones in the matrix I want the column name to appear and >where there are zeros nothing, which would make the vector in this >shortened case: > >combinations <- ("A B","A C","A E H","A F G","B C D","E G H",A C D E H","A >C D F G") > > no value A B C D E F G H >1 2 3.095 1 1 0 0 0 0 0 0 >2 2 1.687 1 0 1 0 0 0 0 0 >46 3 3.470 1 0 0 0 1 0 0 1 >47 3 1.563 1 0 0 0 0 1 1 0 >50 3 6.234 0 1 1 1 0 0 0 0 >148 4 3.663 0 0 1 0 1 0 1 1 >151 4 3.470 0 0 0 1 1 1 0 1 >177 5 5.411 1 0 1 1 1 0 0 1 >178 5 6.829 1 0 1 1 0 1 1 0 > >Question is how to make this not so manually? > >--------------------------------------------- >Henrik Andersson >Netherlands Institute of Ecology - >Centre for Estuarine and Marine Ecology >P.O. Box 140 >4400 AC Yerseke >Phone: +31 113 577473 >h.andersson at nioo.knaw.nl >http://www.nioo.knaw.nl/ppages/handersson > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlEric Lecoutre UCL / Institut de Statistique Voie du Roman Pays, 20 1348 Louvain-la-Neuve Belgium tel: (+32)(0)10473050 lecoutre at stat.ucl.ac.be http://www.stat.ucl.ac.be/ISpersonnel/lecoutre If the statistics are boring, then you've got the wrong numbers. -Edward Tufte
Andy Bunn
2004-Nov-23 16:39 UTC
[R] Create a vector of combinations based on a table column names
There has to be a better (more readable) way, but this works...> set.seed(323) > foo.df <- data.frame(A = round(runif(5)), B = round(runif(5)), C round(runif(5))) > foo.dfA B C 1 0 1 1 2 1 1 1 3 1 1 1 4 0 1 1 5 1 1 0> names.list <- lapply( apply( foo.df, 1, function( x ) colnames(foo.df )[ as.logical( x ) ] ), paste, collapse = ", " )> names.vect <- unlist(names.list) > foo.dfA B C 1 0 1 1 2 1 1 1 3 1 1 1 4 0 1 1 5 1 1 0> names.vect1 2 3 4 5 "B, C" "A, B, C" "A, B, C" "B, C" "A, B">Those nested applies make my head hurt. HTH, Andy> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Henrik Andersson > Sent: Tuesday, November 23, 2004 10:41 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Create a vector of combinations based on a table column > names > > > I want to create a character vector based on the table (shortened for > display) below: > Where there are ones in the matrix I want the column name to appear and > where there are zeros nothing, which would make the vector in this > shortened case: > > combinations <- ("A B","A C","A E H","A F G","B C D","E G H",A C D E > H","A C D F G") > > no value A B C D E F G H > 1 2 3.095 1 1 0 0 0 0 0 0 > 2 2 1.687 1 0 1 0 0 0 0 0 > 46 3 3.470 1 0 0 0 1 0 0 1 > 47 3 1.563 1 0 0 0 0 1 1 0 > 50 3 6.234 0 1 1 1 0 0 0 0 > 148 4 3.663 0 0 1 0 1 0 1 1 > 151 4 3.470 0 0 0 1 1 1 0 1 > 177 5 5.411 1 0 1 1 1 0 0 1 > 178 5 6.829 1 0 1 1 0 1 1 0 > > Question is how to make this not so manually? > > --------------------------------------------- > Henrik Andersson > Netherlands Institute of Ecology - > Centre for Estuarine and Marine Ecology > P.O. Box 140 > 4400 AC Yerseke > Phone: +31 113 577473 > h.andersson at nioo.knaw.nl > http://www.nioo.knaw.nl/ppages/handersson > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >