Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading