用 R 重新编码变量 [英] Recoding variables with R

查看:51
本文介绍了用 R 重新编码变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 R 中重新编码变量,似乎是我最头疼的问题.您使用哪些功能、包、流程来确保最佳结果?

Recoding variables in R, seems to be my biggest headache. What functions, packages, processes do you use to ensure the best result?

我在 Internet 上找到了很少有用的示例,它们提供了一种通用的重新编码解决方案,我很想看看你们和小伙伴们在使用什么.

I've found very few useful examples on the Internet that give a one-size-fits-all solution to recoding and I'm interested to see what you guys and gals are using.

注意:这可能是社区维基主题.

Note: This may be a community wiki topic.

推荐答案

重新编码意味着很多事情,而且从根本上来说是复杂的.

Recoding can mean a lot of things, and is fundamentally complicated.

可以使用 levels 函数更改因子的级别:

Changing the levels of a factor can be done using the levels function:

> #change the levels of a factor
> levels(veteran$celltype) <- c("s","sc","a","l")

转换连续变量只涉及应用向量化函数:

Transforming a continuous variable simply involves the application of a vectorized function:

> mtcars$mpg.log <- log(mtcars$mpg) 

对于分箱连续数据,请查看 cutcut2(在 hmisc 包中).例如:

For binning continuous data look at cut and cut2 (in the hmisc package). For example:

> #make 4 groups with equal sample sizes
> mtcars[['mpg.tr']] <- cut2(mtcars[['mpg']], g=4)
> #make 4 groups with equal bin width
> mtcars[['mpg.tr2']] <- cut(mtcars[['mpg']],4, include.lowest=TRUE)

为了将连续变量或因子变量重新编码为分类变量,car 包中有 recode,Deducer 包中有 recode.variables

For recoding continuous or factor variables into a categorical variable there is recode in the car package and recode.variables in the Deducer package

> mtcars[c("mpg.tr2")] <- recode.variables(mtcars[c("mpg")] , "Lo:14 -> 'low';14:24 -> 'mid';else -> 'high';")

如果您正在寻找 GUI,Deducer 会使用 Transform 和 Recode 对话框实现重新编码:

If you are looking for a GUI, Deducer implements recoding with the Transform and Recode dialogs:

http://www.deducer.org/pmwiki/pmwiki.php?n=Main.TransformVariables

http://www.deducer.org/pmwiki/pmwiki.php?n=Main.RecodeVariables

这篇关于用 R 重新编码变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆