[R] Another newbie question

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Wed Jan 7 20:57:20 CET 2009


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




More information about the R-help mailing list