Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr if else without the else

Tags:

r

dplyr

I have a dataset where I'm trying to change the values of some variables based on a different variable with an if else statement. However, I only want to change the variable if a certain condition is met - otherwise I want the variable to be unchanged. How do I do this in dplyr?

e.g., if I have 4 sites (a, b, c and d), that are each associated with a value of 10, 20, 30 and 40, respectively, and I just want to change the value of 10 at site a to 12.

df2 <- df %>%
  mutate(lat = ifelse(site == "a", 12, WHAT GOES HERE?))
like image 247
tnt Avatar asked Jan 25 '19 19:01

tnt


1 Answers

df2 <- df %>%
  mutate(lat = ifelse(site == "a", 12, lat))
like image 75
NelsonGon Avatar answered Sep 22 '22 05:09

NelsonGon