Camilo Mora
2013-Mar-02 10:26 UTC
[R] 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/
Anthony Damico
2013-Mar-02 11:10 UTC
[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@dal.ca> wrote:> 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/ <http://www.soc.hawaii.edu/mora/> > > ______________________________**________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide http://www.R-project.org/** > posting-guide.html <http://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Rui Barradas
2013-Mar-02 11:56 UTC
[R] replace zeros for NA in a column based on values of another column
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 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/ > > ______________________________________________ > 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.