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)
: Thursday, 8 January 2009 7:28 a.m.
To: r-help@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
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)
x
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
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)
4 matches
Mail list logo