Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

r - 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.

question from:https://stackoverflow.com/questions/65861598/how-to-use-mutate-at-with-ifelse

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...