是否有替代“重估"的方法?使用 dplyr 时来自 plyr 的功能? [英] Is there an alternative to "revalue" function from plyr when using dplyr?

查看:13
本文介绍了是否有替代“重估"的方法?使用 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?

推荐答案

有一个 recode 功能可用,从 dplyr 版本 dplyr_0.5.0 开始,它看起来非常类似于 <代码>重估来自plyr.

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

recode 文档Examples 部分构建的示例:

Example built from the recode documentation Examples section:

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"

可以使用 .default 参数更改此行为.

This behavior can be changed using the .default argument.

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天全站免登陆