In a dplyr workflow I try to paste a 0 in each column of a dataframe after the newvar column when newvar == 0, else do nothing. I modified the iris dataset:
library(dplyr)
n <- 150 # sample size
iris1 <- iris %>%
mutate(id = row_number(), .before = Sepal.Length) %>%
mutate(newvar = sample(c(0,1), replace=TRUE, size=n), .before = Sepal.Length ) %>%
mutate(across(.[,3:ncol(.)], ~ case_when(newvar==0 ~ 0)))
I tried a solution like here How to combine the across () function with mutate () and case_when () to mutate values in multiple columns according to a condition?. My understanding:
- with
.[,3:ncol(.)]
I go through the columns after newvar column. - with
case_when(newvar==0
I try to set the condition. - with
~ 0
afternewvar==0
I try to say paste 0 if condition is fulfilled.
I know that I am doing something wrong, but I don't know what! Thank you for your help.
.[,3:ncol(.)]
.3:ncol(.)
works just fine. – Carlos Davila3:ncol(.)
with their original value. Now these cells get a NA. So the question is: If newrow==1 change nothing in3:coll(.)
– TarJae