If i want to replace all the negative values as well as values of (999,9991,9992,9996) for all the columns of my data frame with -100, how should i do it. I want to save it as a new data frame and not on the same data frame. in my data set i have hundreds of columns
x<-c(-1,-2,0,1,9991)
y<-c(1,2,3,4,999)
z<-c(100,101,-999,9992,9996)
data<-as.data.frame(cbind(x,y,z))
A solution using lapply
to replace values in each column.
data2 <- data
data2[] <- lapply(data2, function(x){
x[x < 0 | x %in% c(999, 9991, 9992, 9996)] <- -100
return(x)
})
data2
# x y z
# 1 -100 1 100
# 2 -100 2 101
# 3 0 3 -100
# 4 1 4 -100
# 5 -100 -100 -100
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.