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

How to change a certain value in a column of a list into NA in R?

I would like to change -1 value of my V3 column into NA, but don’t know how…

mat2 <- matrix(sample(-1:100,50,replace = TRUE),ncol = 5)  
mat2 <- as.data.frame(mat2)
mat2[8,3] <- -1
mat2[1,3] <- -1
mat2[5,3] <- -1

mat2 <- matrix(sample(-1:100,50,replace = TRUE),ncol = 5)  
mat2 <- as.data.frame(mat2)
mat2[8,3] <- -1
mat2[1,3] <- -1
mat2[5,3] <- -1

mat3 <- matrix(sample(-1:100,50,replace = TRUE),ncol = 5)  
mat3 <- as.data.frame(mat2)
mat3[2,3] <- -1
mat3[4,3] <- -1
mat3[8,3] <- -1

data <- list(mat1, mat2, mat3)

Can anyone help please?
Thanks in advance.

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 :

You can use this code:

lapply(data, function(d) {d$V3[d$V3 == -1]  <- "NA"; d}) 
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