有没有替代“重估”价值的方法?使用dplyr时从plyr转换功能? [英] Is there an alternative to "revalue" function from plyr when using dplyr?

查看:73
本文介绍了有没有替代“重估”价值的方法?使用dplyr时从plyr转换功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 revalue 函数的粉丝,它是 plyr 来代替字符串。

I'm a fan of the revalue function is plyr for substituting strings. It's simple and easy to remember.

但是,我已经将新代码迁移到了未显示的 dplyr 上。具有 revalue 函数。 dplyr 中接受的习惯用法是以前用 revalue 完成的工作?

However, I've migrated new code to dplyr which doesn't appear to have a revalue function. What is the accepted idiom in dplyr for doing things previously done with revalue?

推荐答案

dplyr 版本dplyr_0.5.0开始,有一个 recode 函数。看起来非常类似于 plyr 中的 revalue

There is a recode function available starting with dplyr version dplyr_0.5.0 which looks very similar to revalue from plyr.

根据<$ c构建的示例$ c>重新编码文档示例部分:

set.seed(16)
x = sample(c("a", "b", "c"), 10, replace = TRUE)
x
 [1] "a" "b" "a" "b" "b" "a" "c" "c" "c" "a"

recode(x, a = "Apple", b = "Bear", c = "Car")

   [1] "Car"   "Apple" "Bear"  "Apple" "Car"   "Apple" "Apple" "Car"   "Car"   "Apple"

如果仅定义要重新编码的某些值,默认情况下,其余的将用 NA

If you only define some of the values that you want to recode, by default the rest are filled with NA.

recode(x, a = "Apple", c = "Car")
 [1] "Car"   "Apple" NA      "Apple" "Car"   "Apple" "Apple" "Car"   "Car"   "Apple"

可以使用<$ c更改此行为$ c> .default 参数。

recode(x, a = "Apple", c = "Car", .default = x)
 [1] "Car"   "Apple" "b"     "Apple" "Car"   "Apple" "Apple" "Car"   "Car"   "Apple"

如果要替换,还有一个 .missing 参数缺少其他值。

There is also a .missing argument if you want to replace missing values with something else.

这篇关于有没有替代“重估”价值的方法?使用dplyr时从plyr转换功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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