Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update values with dplyr

Tags:

r

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

like image 767
droopy Avatar asked Apr 15 '14 08:04

droopy


People also ask

How do I change the value of a dplyr?

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.

How do I replace specific values in R?

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.

How do I update a row in R?

To add a new row to the existing dataframe, we can use the function rbind().

How do you mutate variables in R?

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.


1 Answers

Use mutate and ifelse

mutate(net4,   ave = ifelse(ave < 10 & ave != temp2, temps2 / NNET * NET, ave) ) 
like image 175
hadley Avatar answered Sep 22 '22 00:09

hadley