tnt
tnt

Reputation: 1459

dplyr if else without the else

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?))

Upvotes: 9

Views: 5493

Answers (1)

NelsonGon
NelsonGon

Reputation: 13309

df2 <- df %>%
  mutate(lat = ifelse(site == "a", 12, lat))

Upvotes: 11

Related Questions