Home > Software design >  How to change a certain value in a column of a list into NA in R?
How to change a certain value in a column of a list into NA in R?

Time:03-02

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.

CodePudding user response:

You can use this code:

lapply(data, function(d) {d$V3[d$V3 == -1]  <- "NA"; d}) 
  • Related