Search code examples
rradix

Replace specific value in R


I know it's something it should be easy but somehow I am stuck I am trying to eliminate an outlier, replacing it for NA dat is my data, and the value is what I want to modify

dat[dat$i_huvisfatin_v00 == 16527.98, "i_huvisfatin_v00"] <- NA

Error in `[<-.data.frame`(`*tmp*`, dat$i_huvisfatin_v00 == 16527.98, "i_huvisfatin_v00",  : 
  missing values are not allowed in subscripted assignments of data frames

dat[dat$i_huvisfatin_v00 == "16527.98", "i_huvisfatin_v00"] <- NA

Error in `[<-.data.frame`(`*tmp*`, dat$i_huvisfatin_v00 == 16527.98, "i_huvisfatin_v00",  : 
  missing values are not allowed in subscripted assignments of data frames


What am I doing wrong?


Solution

  • From your error message, it's either your dat$i_huvisfatin_v00 column doesn't contain the value 16527.98, or it already have NA in the column.

    dat$i_huvisfatin_v00 == 16527.98 returns a logical vector, which cannot be treated as index itself if it contains NA. Use which() in the row index seems to solve the problem.

    dat[which(dat$i_huvisfatin_v00 == 16527.98), "i_huvisfatin_v00"] <- NA