使用dplyr替换选定列上的值 [英] Use dplyr to replace values on select columns

查看:804
本文介绍了使用dplyr替换选定列上的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在dplyr中使用replace()函数来清理我的数据。我想在除了一个之外的所有列上运行它。如果我在丢失字符标识符之前使用了select()语句。我正在寻找这样的东西

I am trying to use the replace() function in dplyr to clean my data. I want to run it on all the columns except one. If I use a select() statement before I lose my character identifiers. I am looking for something like this

    newdata<-data %>% replace(((.)>1000),0)

但有一个例外

    newdata<-data %>% replace(((-StoreID)>1000),0)


推荐答案

由于您没有提供可重复的示例,这里是如何在 iris 数据集中工作的:

Since you didn't provide a reproducible example, here's how it would work on the iris dataset:

iris %>% mutate_each(funs(replace(., . > 5, NA)), -Species)

我们使用 mutate_each()替换为 NA 种类之外的所有列中,值大于5的值

对于你的例子,它将是这样的:

For your example it would be something like:

data %>% mutate_each(funs(replace(., . > 1000, 0)), -StoreID)

这篇关于使用dplyr替换选定列上的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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