[R] identifying when one element of a row has a positive number

Daisy Englert Duursma daisy.duursma at gmail.com
Thu Jan 27 06:36:44 CET 2011


Hello,

I am not sure where to begin with this problem or what to search for
in r-help. I just don't know what to call this.

If I have 5 columns, the first 2 are the x,y, locations and the last
three are variables about those locations.

x<-seq(1860,1950,by=10)
y<-seq(-290,-200,by=10)
ANN<-c(3,0,0,0,1,0,1,1,0,0)
CTA<-c(0,1,0,0,0,0,1,0,0,2)
GLM<-c(0,0,2,0,0,0,0,1,0,0)
df1<-as.data.frame(cbind(x,y,ANN,CTA,GLM))

What I would like to produce is an additional column that tells when
only 1 of the three variables has a value greater than 0. I would like
this new column to give the name of the variable. Likewise, I would
like a column that tells one only one of the three variables for a
given row has a value of 0. For my example the new columns would be:

one_presence<-c("ANN","CTA","GLM","NA","ANN","NA","NA","NA","NA","CTA")
one_absence<-c("NA","NA","NA","NA","NA","NA","GLM","CTA","NA","NA")

The end result should look like

df2<-(cbind(df1,one_presence,one_absence))

I am sure I can do this with a loop or maybe grep but I am out of ideas.

Any help would be appreciated.

Cheers,
Daisy

-- 
Daisy Englert Duursma

Room E8C156
Dept. Biological Sciences
Macquarie University  NSW  2109
Australia



More information about the R-help mailing list