I am trying to replace all zeros in multiple columns with NA
using dplyr
.
However, since I have many variables, I do not want to call them all by one, but rather store them in an object that I can call afterwards.
This is a minimal example of what I did:
library(dplyr)
Data <- data.frame(var1 = c(1:10), var2 = rep(c(0, 4), 5), var3 = rep(c(2, 0, 3, 4, 5), 2), var4 = rep(c(7, 0), 5))
col <- Data[,c(2:4)]
Data <- Data %>%
mutate(across(col, na_if, 0))
However, if I do this, I get the following error message:
Error: Problem with 'mutate()' input '..1'.
x Must subset columns with a valid subscript vector.
x Subscript has the wrong type 'data.frame<
var2: double
var3: double
var4: double>'.
i It must be numeric or character.
i Input '..1' is '(function (.cols = everything(), .fns = NULL, ..., .names = NULL) ...'.
I have tried to change the format of col
to a tibble, but that did not help.
Could anyone tell me how to make this work?