I wanted to use the new across function from dplyr to select consecutive columns and to change the NA in zeros. However, it does not work. It seems like a very simple thing so it could be that I miss something.
A working example:
> m <- matrix(sample(c(NA, 1:10), 100, replace = TRUE), 10)
> d <- as.data.frame(m)
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1 4 3 NA 3 7 6 6 10 6 5
2 9 8 9 5 10 NA 2 1 7 2
3 1 1 6 3 6 NA 1 4 1 6
4 NA 4 NA 7 10 2 NA 4 1 8
5 1 2 4 NA 2 6 2 6 7 4
6 NA 3 NA NA 10 2 1 10 8 4
7 4 4 9 10 9 8 9 4 10 NA
8 5 8 3 2 1 4 5 9 4 7
9 3 9 10 1 9 9 10 5 3 3
10 4 2 2 5 NA 9 7 2 5 5
This works fine:
mutate_at(vars(V1:V4), ~replace(., is.na(.), 0))
But if try these options I get an error:
d %>% mutate(across(vars(V1:V4)), ~replace(., is.na(.), 0))
d %>% mutate(across(V1:V4)), ~replace(., is.na(.), 0))
d %>% mutate(across("V1":"V4")), ~replace(., is.na(.), 0))
I am not sure why this doesn't work