如何使用dplyr更新值 [英] How to update values with dplyr

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

问题描述

我正在尝试使用dplyr从data.frame更新值,但我不知道是否可以替换值的一个子集?

I'm currently trying to update values from a data.frame using dplyr butI don't know if it is possible to replace a subset of values?

# the net4 table
head(net4)
Source: local data frame [6 x 4]

  temps2 NNET NET ave
1     18    2   4  36
2     18    2   4  36
3     22    2   4  44
4     18    2   4  36
5     22    2   4  44
6     27    3   4  36

# I would like to do the same command line as below:
subs <- (net4$ave < 10 & net4$ave!=net4$temps2)
net4$ave[subs] <- with(net4[subs,], temps2/NNET*NET)

谢谢

推荐答案

使用 mutate ifelse

mutate(net4,
  ave = ifelse(ave < 10 & ave != temp2, temps2 / NNET * NET, ave)
)

这篇关于如何使用dplyr更新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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