How to select consecutive columns with across function dplyr

Viewed 369

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

1 Answers

In across(), there are two basic arguments. The first argument are the columns that are to be modified, while the second argument is the function which should be applied to the columns. In addition, vars() is no longer needed to select the variables. Thus, the correct form is:

d %>%
 mutate(across(V1:V4, ~ replace(., is.na(.), 0)))

   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1   2  6  0  6  5  6 10  5  3   1
2   2  9  2  4 10  6  9  4 NA  NA
3   5  5  3  0  3  7  1  5  9   5
4   7  1  1  6  2  1  8 NA  8   4
5   3  5  3  0  2  3  4  2  3  NA
6   0 10  0  2  5 10  1 10  4   3
7   4  3 10  6 NA  5  9  3  3   9
8   9  9  8  5  8  1  3  1 NA  10
9   6  3  0  1  1  9  3  5  8   4
10  3  2  9  1  5  2  4 NA  6   1
Related