Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

r - How to update values with dplyr

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)

Thanks

question from:https://stackoverflow.com/questions/23078891/how-to-update-values-with-dplyr

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use mutate and ifelse

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...