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
Use mutate() and its other verbs mutate_all() , mutate_if() and mutate_at() from dplyr package to replace/update the values of the column (string, integer, or any type) in R DataFrame (data. frame). For more methods of this package refer to the R dplyr tutorial.
replace() function in R Language is used to replace the values in the specified string vector x with indices given in list by those given in values. It takes on three parameters first is the list name, then the index at which the element needs to be replaced, and the third parameter is the replacement values.
To add a new row to the existing dataframe, we can use the function rbind().
In R programming, the mutate function is used to create a new variable from a data set. In order to use the function, we need to install the dplyr package, which is an add-on to R that includes a host of cool functions for selecting, filtering, grouping, and arranging data.
Use mutate
and ifelse
mutate(net4, ave = ifelse(ave < 10 & ave != temp2, temps2 / NNET * NET, ave) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With