Problem: I have a data frame with 1s and 0s denoting presence/absence of species (columns) for particular plot measurements (rows). What I want to do is make a new column whose entries for each row is a list of the column names in which a species is present (ie. for row one its entry might read: "sp1","sp2", etc.). I've tried various functions etc. but can't seem to get the syntax right/ the correct combination of functions. Thanks in advance! -Allen -- View this message in context: http://www.nabble.com/Another-newbie-question-tp21337371p21337371.html Sent from the R help mailing list archive at Nabble.com.
Kia ora Allen I'm not sure what you have tried or what your level of understanding is. However, if your dataframe is:> allensp1 sp2 sp3 1 0 0 1 2 1 0 1 3 1 1 1 4 0 0 0> str(allen)'data.frame': 4 obs. of 3 variables: $ sp1: int 0 1 1 0 $ sp2: int 0 0 1 0 $ sp3: int 1 1 1 0 Then looking at the output from: paste(names(allen)[as.logical(unlist(allen[2,]))], collapse='; ') may help you. Hei kona ra ... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of AllenL > Sent: Thursday, 8 January 2009 7:28 a.m. > To: r-help at r-project.org > Subject: [R] Another newbie question > > > Problem: > I have a data frame with 1s and 0s denoting presence/absence > of species > (columns) for particular plot measurements (rows). What I > want to do is make a new column whose entries for each row is > a list of the column names in which a species is present (ie. > for row one its entry might read: > "sp1","sp2", etc.). I've tried various functions etc. but > can't seem to get the syntax right/ the correct combination > of functions. > Thanks in advance! > -Allen > > -- > View this message in context: > http://www.nabble.com/Another-newbie-question-tp21337371p21337371.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >The contents of this e-mail are confidential and may be subject to legal privilege. If you are not the intended recipient you must not use, disseminate, distribute or reproduce all or any part of this e-mail or attachments. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail. Any opinion or views expressed in this e-mail are those of the individual sender and may not represent those of The New Zealand Institute for Plant and Food Research Limited.
You did not provide any data, so I will take a guess at what it looks like:> x <- matrix(sample(0:1, 100, TRUE), 10) > colnames(x) <- LETTERS[1:10] > x <- as.data.frame(x) > xA B C D E F G H I J 1 0 1 0 0 0 1 1 0 0 0 2 0 0 0 0 1 1 0 0 1 0 3 1 1 0 0 1 0 0 0 1 1 4 0 1 1 1 0 1 1 0 0 0 5 0 1 1 0 0 0 1 0 0 1 6 1 0 1 1 0 0 0 0 0 1 7 1 1 0 1 0 0 0 0 0 0 8 0 1 0 1 0 0 1 0 0 0 9 0 1 0 0 0 0 1 0 0 0 10 1 0 1 0 1 1 0 1 0 1> x$match <- apply(x, 1, function(z) paste(colnames(x)[z == 1], collapse=",")) > > > xA B C D E F G H I J match 1 0 1 0 0 0 1 1 0 0 0 B,F,G 2 0 0 0 0 1 1 0 0 1 0 E,F,I 3 1 1 0 0 1 0 0 0 1 1 A,B,E,I,J 4 0 1 1 1 0 1 1 0 0 0 B,C,D,F,G 5 0 1 1 0 0 0 1 0 0 1 B,C,G,J 6 1 0 1 1 0 0 0 0 0 1 A,C,D,J 7 1 1 0 1 0 0 0 0 0 0 A,B,D 8 0 1 0 1 0 0 1 0 0 0 B,D,G 9 0 1 0 0 0 0 1 0 0 0 B,G 10 1 0 1 0 1 1 0 1 0 1 A,C,E,F,H,J>On Wed, Jan 7, 2009 at 1:28 PM, AllenL <allen.larocque at gmail.com> wrote:> > Problem: > I have a data frame with 1s and 0s denoting presence/absence of species > (columns) for particular plot measurements (rows). What I want to do is make > a new column whose entries for each row is a list of the column names in > which a species is present (ie. for row one its entry might read: > "sp1","sp2", etc.). I've tried various functions etc. but can't seem to get > the syntax right/ the correct combination of functions. > Thanks in advance! > -Allen > > -- > View this message in context: http://www.nabble.com/Another-newbie-question-tp21337371p21337371.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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?
try the following: dat <- data.frame( sp1 = rbinom(10, 1, 0.5), sp2 = rbinom(10, 1, 0.5), sp3 = rbinom(10, 1, 0.5), sp4 = rbinom(10, 1, 0.5), sp5 = rbinom(10, 1, 0.5), sp6 = rbinom(10, 1, 0.5) ) ind <- sapply(dat, as.logical) dat$Sp <- apply(ind, 1, function (x, nams) paste(nams[x], collapse = "; "), nams = names(dat)) dat I hope it helps. Best, Dimitris AllenL wrote:> Problem: > I have a data frame with 1s and 0s denoting presence/absence of species > (columns) for particular plot measurements (rows). What I want to do is make > a new column whose entries for each row is a list of the column names in > which a species is present (ie. for row one its entry might read: > "sp1","sp2", etc.). I've tried various functions etc. but can't seem to get > the syntax right/ the correct combination of functions. > Thanks in advance! > -Allen >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
Thank you all! In future I will include examples of my code to make things simpler for you. This is what I settled on: Sp.presence<-Data[,14:31] ##The subset of my data set I'm interested in (the presence/absence data) Sp.presence$Species<-apply(Sp.presence,1,function(x) {c(paste(names(Sp.presence)[x==1],collapse=","))}) Yay! -Allen AllenL wrote:> > Problem: > I have a data frame with 1s and 0s denoting presence/absence of species > (columns) for particular plot measurements (rows). What I want to do is > make a new column whose entries for each row is a list of the column names > in which a species is present (ie. for row one its entry might read: > "sp1","sp2", etc.). I've tried various functions etc. but can't seem to > get the syntax right/ the correct combination of functions. > Thanks in advance! > -Allen > >-- View this message in context: http://www.nabble.com/Another-newbie-question-tp21337371p21360273.html Sent from the R help mailing list archive at Nabble.com.