简体   繁体   中英

Dplyr: using mutate , across , where and ìfelse to multiple entire column in R

This is my code:

library(vcd)
data(Arthritis)

df<-as.tibble(Arthritis)

df %>% mutate(across(where(~ is.integer(.x) && first(.x) > 50), ~ ifelse(is.integer(.x), .x * 2, .x)))

I would like multiply the entire column that has as the first row a number above 50 and is an integer .

But why its not working? Probably because Im using first(.x) .
What am I doing wrong by using first ?

Many thanks

Move the first() logic out of the selection and into the operation, don't duplicate the is.integer check:

df %>% 
  mutate(across(where(is.integer),
         ~ if(first(.x) > 50) {.x * 2} else{.x})
  )

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