R:如何一次重新编码多个变量 [英] R: How to recode multiple variables at once
问题描述
我的数据集中有几个变量需要以完全相同的方式重新编码,而其他几个变量则需要以不同的方式重新编码。我尝试编写一个函数来解决此问题,但是遇到了麻烦。
I have several variables in my dataset that need to be recoded in exactly the same way, and several other variables that need to be recoded in a different way. I tried writing a function to help me with this, but I'm having trouble.
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 studio为此工作约5分钟然后给我这个错误消息:
R studio works on this for about 5 minutes then gives me this error message:
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
任何帮助将不胜感激!谢谢
Any help would be really appreciated! Thank you
推荐答案
我认为与 dplyr 相比,这更整洁。正确使用 recode
是个好主意。 mutate_all()
可用于对整个数据帧进行操作, mutate_at()
仅可用于所选变量。在 dplyr 中有很多指定变量的方法。
This is neater I think with dplyr. Using recode
correctly is a good idea. mutate_all()
can be used to operate on the whole dataframe, mutate_at()
on just selected variables. There are lots of ways to specify variables in 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,因为它是数字,易于管理在其他数字列中。
I use NaN instead of NA as it is numeric is be simpler to manage within a column of other numbers.
这篇关于R:如何一次重新编码多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!