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
Kia ora Allen
I'm not sure what you have tried or what your level of understanding is.
However, if your dataframe is:
allen
sp1 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.
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.
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.
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