40
votes

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

1

1 Answers

50
votes

Use mutate and ifelse

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