Skip to content

replace zeros for NA in a column based on values of another column

6 messages · Camilo Mora, Anthony Damico, Petr Suvorov (Quetzalcoatlus) +2 more

#
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/
#
Hello,

Try

# for columns a.b it's 1:2, not 1:3
data[data[,4] == 0, 1:3] <- NA  # columns a, b and c


Hope this helps,

Rui Barradas

Em 02-03-2013 10:26, Camilo Mora escreveu:
#
HI,

Not sure I understand it correctly,

data1<-read.table(text="
?a????? b????? c????? d
?0????? 1????? 1????? 0
?1????? 1????? 1????? 1
?1????? 0????? 0????? 1
",sep="",header=TRUE)
data2<- data1
data3<- data1
If i follow this logic for the 1st and 2nd columns,
?data1[ data1[ , 4 ] == 0 , 1 ] <- NA
?data1[ data1[ , 4 ] == 0 , 2 ] <- NA
?data1
#?? a? b c d
#1 NA NA 1 0
#2? 1? 1 1 1
#3? 1? 0 0 1

Still, the column 'b' with 0 element is left as such while and `1` in b is replaced with NA

? data2[,1][data2[,4]==0 & data2[,1]==0]<- NA
?data2[,2][data2[,4]==0 & data2[,2]==0]<- NA
?data2
#?? a b c d
#1 NA 1 1 0
#2? 1 1 1 1
#3? 1 0 0 1

#or you can try
data3[,1:2]<- lapply(letters[1:2],function(x) {x1<-cbind(data3[,x],data3[,4]);colnames(x1)<- c(x,"d");x1; x1[rowSums(x1)==0,1]<-NA;x1[,1]})
?data3
#?? a b c d
#1 NA 1 1 0
#2? 1 1 1 1
#3? 1 0 0 1

A.K.




----- Original Message -----
From: Anthony Damico <ajdamico at gmail.com>
To: Camilo Mora <cmora at dal.ca>
Cc: R help <r-help at r-project.org>
Sent: Saturday, March 2, 2013 6:10 AM
Subject: Re: [R] replace zeros for NA in a column based on values of another column

you want to replace all rows where the 4th column is zero..? (data[ , 4 ]
== 0)
and you want to perform that replacement in the first column..

so try

data[ data[ , 4 ] == 0 , 1 ] <- NA
On Sat, Mar 2, 2013 at 5:26 AM, Camilo Mora <cmora at dal.ca> wrote:

            
??? [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
#
Hello,

Arun has a point, I forgot to check the element to be replaced for a 
zero. The following code will do that.

dat <- read.table(text = "
a       b       c       d
0       1       1       0
1       1       1       1
1       0       0       1
", header = TRUE)
dat <- data.matrix(dat)
str(dat)

dat[, 1:2] <- sapply(1:2, function(i) ifelse(dat[, i] == 0 & dat[, 4] == 
0, NA, dat[, i]))


Hope this helps,

Rui Barradas

Em 02-03-2013 16:11, arun escreveu: