Skip to content

Another newbie question

5 messages · AllenL, Peter Alspach, jim holtman +1 more

#
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:
sp1 sp2 sp3
1   0   0   1
2   1   0   1
3   1   1   1
4   0   0   0
'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
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:
A 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
A 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:

  
    
#
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:

  
    
1 day later
#
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: