重新编码/重新调整不同级别的 data.frame 因子
每次当我必须重新编码一组变量时,我都会想到 SPSS 重新编码功能。我必须承认这非常简单。 car
包中有一个类似的 recode
函数,它也能达到目的,但我们假设我想用 factor
来完成任务。
我有 data.frame
,其中包含多个值范围从 1 到 7 的变量。我想“反转”变量值,因此将 1s 替换为 7s,将 2s 替换为 6s,将 3s 替换为 5s 等。我可以利用factor
函数:
# create dummy factor
set.seed(100)
x <- as.factor(round(runif(100,1,7)))
y <- factor(x, levels = rev(levels(x)))
如果我运行:
> levels(x)
[1] "1" "2" "3" "4" "5" "6" "7"
> levels(y)
[1] "7" "6" "5" "4" "3" "2" "1"
当我想重新编码不具有相同级别的因子时,问题就开始了。如果某个因子 z 具有级别 c("1", "3", "4", "6", "7")
,我是否有机会“反转”级别,以便利用factor
函数计算1=7、2=6、3=5等?
其他有效的重新编码功能应该足够了!
Each time when I have to recode some set of variables, I have SPSS recode function in mind. I must admit that it's quite straightforward. There's a similar recode
function in car
package, and it does the trick, but let's presuppose that I want to get things done with factor
.
I have data.frame
with several variables with value range from 1 to 7. I want to "reverse" variable values, hence replacing 1s with 7s, 2s with 6s, 3s with 5s etc. I can utilize factor
function:
# create dummy factor
set.seed(100)
x <- as.factor(round(runif(100,1,7)))
y <- factor(x, levels = rev(levels(x)))
And if I run:
> levels(x)
[1] "1" "2" "3" "4" "5" "6" "7"
> levels(y)
[1] "7" "6" "5" "4" "3" "2" "1"
Problem starts when I want to recode factors that do not have equal levels. If some factor, z, has levels c("1", "3", "4", "6", "7")
, is there any chance that I can "reverse" levels so 1=7, 2=6, 3=5 etc. by utilizing factor
function?
Other efficient recode functions should suffice!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须为因子提供
levels
参数(如 Dirk 所写):您也可以对现有因子执行此操作,
正如您所看到的级别已按所需顺序扩展。
You must provide
levels
argument to factor (as Dirk wrote):you could do this on existing factor too
As you see levels were extended in desire order.
是的,只需分配到
级别
:Yes, just assign to
levels
:如果您完成了因子级别,那么您就可以开始:
If you complete the factor levels you're good to go:
在这种情况下,既然你有数字,为什么不直接使用模运算来转换数字呢?
例如,
如果使用更大的范围,请适当修改 6 和 7。
In this case, since you have numbers, why not just transform the numbers using modular arithmetic?
eg
Modify the 6 and 7 as appropriate if using larger ranges.