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
______________________________________________
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.