R:如何一次重新编码多个变量
R: How to recode multiple variables at once
我的数据集中有几个变量需要以完全相同的方式重新编码,还有几个其他变量需要以不同的方式重新编码。我尝试编写一个函数来帮助我解决这个问题,但我遇到了麻烦。
library(dplyr)
recode_liberalSupport = function(arg1){
arg1 = recode(arg1, "1=-1;2=1;else=NA")
return(arg1)
}
liberals = c(df$var1, df$var4, df$var8)
for(i in unique(liberals)){
paste(df$liberals[i] <- sapply(liberals, FUN = recode_liberalSupport))
}
R 工作室为此工作了大约 5 分钟,然后给我这个错误消息:
Error in `$<-.data.frame`(`*tmp*`, liberals, value = c(NA_real_, NA_real_, :
replacement has 9 rows, data has 64600
In addition: Warning messages:
1: Unknown or uninitialised column: 'liberals'.
2: In df$liberals[i] <- sapply(liberals, FUN = recode_liberalSupport) :
number of items to replace is not a multiple of replacement length
任何帮助将不胜感激!谢谢
一如既往,有很多方法可以做到这一点。我不太了解 dplyr,无法使用该功能,但这似乎是您要找的。
mydata <- data.frame(arg1=c(1,2,4,5),arg2=c(1,1,2,0))
mydata
arg1 arg2
1 1 1
2 2 1
3 4 2
4 5 0
使用嵌套 ifelse()
重新编码的函数
recode_liberalSupport <- function(var = "arg1", data=mydata) {
+ recoded <- ifelse(mydata[[var]] == 1, -1,
+ ifelse(mydata[[var]] == 2, 1, NA))
+ return(recoded)
+ }
调用函数
recode_liberalSupport(var = "arg1")
[1] -1 1 NA NA
用重新编码的值替换变量 arg1
。
mydata$arg1 <- recode_liberalSupport(var = "arg1")
mydata
arg1 arg2
1 -1 1
2 1 1
3 NA 2
4 NA 0
我认为 dplyr 更简洁。正确使用 recode
是个好主意。 mutate_all()
可用于对整个数据框进行操作,mutate_at()
仅对选定的变量进行操作。在 dplyr.
中有很多指定变量的方法
mydata <- data.frame(arg1=c(1,2,4,5),arg2=c(1,1,2,0),arg3=c(1,1,1,1))
mydata
arg1 arg2 arg3
1 1 1 1
2 2 1 1
3 4 2 1
4 5 0 1
mydata <- mydata %>%
mutate_at(c("arg1","arg2"), funs(recode(., `1`=-1, `2`=1, .default = NaN)))
mydata
arg1 arg2 arg3
1 -1 -1 1
2 1 -1 1
3 NaN 1 1
4 NaN NaN 1
我使用 NaN 而不是 NA,因为它是数字,在其他数字的列中更易于管理。
我的数据集中有几个变量需要以完全相同的方式重新编码,还有几个其他变量需要以不同的方式重新编码。我尝试编写一个函数来帮助我解决这个问题,但我遇到了麻烦。
library(dplyr)
recode_liberalSupport = function(arg1){
arg1 = recode(arg1, "1=-1;2=1;else=NA")
return(arg1)
}
liberals = c(df$var1, df$var4, df$var8)
for(i in unique(liberals)){
paste(df$liberals[i] <- sapply(liberals, FUN = recode_liberalSupport))
}
R 工作室为此工作了大约 5 分钟,然后给我这个错误消息:
Error in `$<-.data.frame`(`*tmp*`, liberals, value = c(NA_real_, NA_real_, :
replacement has 9 rows, data has 64600
In addition: Warning messages:
1: Unknown or uninitialised column: 'liberals'.
2: In df$liberals[i] <- sapply(liberals, FUN = recode_liberalSupport) :
number of items to replace is not a multiple of replacement length
任何帮助将不胜感激!谢谢
一如既往,有很多方法可以做到这一点。我不太了解 dplyr,无法使用该功能,但这似乎是您要找的。
mydata <- data.frame(arg1=c(1,2,4,5),arg2=c(1,1,2,0))
mydata
arg1 arg2
1 1 1
2 2 1
3 4 2
4 5 0
使用嵌套 ifelse()
recode_liberalSupport <- function(var = "arg1", data=mydata) {
+ recoded <- ifelse(mydata[[var]] == 1, -1,
+ ifelse(mydata[[var]] == 2, 1, NA))
+ return(recoded)
+ }
调用函数
recode_liberalSupport(var = "arg1")
[1] -1 1 NA NA
用重新编码的值替换变量 arg1
。
mydata$arg1 <- recode_liberalSupport(var = "arg1")
mydata
arg1 arg2
1 -1 1
2 1 1
3 NA 2
4 NA 0
我认为 dplyr 更简洁。正确使用 recode
是个好主意。 mutate_all()
可用于对整个数据框进行操作,mutate_at()
仅对选定的变量进行操作。在 dplyr.
mydata <- data.frame(arg1=c(1,2,4,5),arg2=c(1,1,2,0),arg3=c(1,1,1,1))
mydata
arg1 arg2 arg3
1 1 1 1
2 2 1 1
3 4 2 1
4 5 0 1
mydata <- mydata %>%
mutate_at(c("arg1","arg2"), funs(recode(., `1`=-1, `2`=1, .default = NaN)))
mydata
arg1 arg2 arg3
1 -1 -1 1
2 1 -1 1
3 NaN 1 1
4 NaN NaN 1
我使用 NaN 而不是 NA,因为它是数字,在其他数字的列中更易于管理。