mne*_*nel 14
惯用的方法是[<-在形式中使用
x[index] <- result
如果您正在处理整数/因子或字符变量,那么==将可靠地用于索引,
x <- rep(1:5,3)
x[x==3] <- 1
x[x==4] <- 2
x
## [1] 1 2 1 2 5 1 2 1 2 5 1 2 1 2 5
Run Code Online (Sandbox Code Playgroud)
它car有一个有用的功能recode(它是一个包装器[<-),可以让你在一次调用中组合所有重新编码
例如
library(car)
x <- rep(1:5,3)
xr <- recode(x, '3=1; 4=2')
x
## [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
xr
## [1] 1 2 1 2 5 1 2 1 2 5 1 2 1 2 5
Run Code Online (Sandbox Code Playgroud)
感谢@joran mapvalues从plyr包中提到的另一个包装器[<-
x <- rep(1:5,3)
mapvalues(x, from = c(3,1), to = c(1,2))
Run Code Online (Sandbox Code Playgroud)
plyr::revalue是mapvalues特定 factor或character变量的包装器.