Skip to content
Back to formatted view

Raw Message

Message-ID: <20130302062601.67055rry251hlxss@wm4.dal.ca>
Date: 2013-03-02T10:26:01Z
From: Camilo Mora
Subject: replace zeros for NA in a column based on values of another column

Hi everyone,

Imagine that I have a data frame with four columns:
data<-
a       b       c       d
0       1       1       0
1       1       1       1
1       0       0       1

I want to replace the zeros in columns a:b for NA only for the rows in  
which column d are zero. So

a       b       c       d
NA      1       1       0
1       1       1       1
1       0       0       1

I am trying this:
data[,1:3][data[4] == 0] <- NA
But get this error:

Error in `[<-.data.frame`(`*tmp*`, Data[4] == 0, value = NA) :
   only logical matrix subscripts are allowed in replacement

Does anyone knows the reason of this error or is there an alternative  
to replace the values in one column based on the values of another?

Thanks,

Camilo

Camilo Mora, Ph.D.
Department of Geography, University of Hawaii
http://www.soc.hawaii.edu/mora/