简体   繁体   中英

How to use mutate_at with ifelse

I'm trying to use the mutate_at function with ifelse but it does not work.

As an example here is what I want to achieve

data <- data.frame(
  A = c(1, 2, 3, 4),
  B = c(2, 3, 4, 1),
  C = c(4, 1, 9, 0)
)

data %>%
  mutate(A = ifelse(A == 4, 1, A)) %>%
  mutate(B = ifelse(B == 4, 1, B))

by using the mutate_at function. Any help would be appreciated.

Simply

data %>%
  mutate_at(vars(A, B), ~ ifelse(.x == 4, 1, .x))

But I'm sure there's a duplicate question/answer. You must also show what you've tried and what error messages it showed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM