1

I have a df that has imperfect data in a number of columns. I do not wish to delete those columns but rather set each value in all of those columns to NA so that I can still have them in the data frame. I am using dplyr and am aware that I could simply list every row and set as NA like:

df %>%
  mutate( column1 = NA,
          column2 = NA, ...

but I would like a cleaner way of grouping all the column together so that I can set that group equal to NA. I've tried c(1:2) = NA (using the column indices) but that hasn't seemed to work. Any help would be appreciated. Thanks in advance.

  • Hi Ben! I wasn't sure if you were familiar with across (or if using across in the title was just a coincidence), or anonymous functions, so I gave a short introduction to them. Let me know if you have any questions! – Mark Aug 22 '23 at 15:40
  • 1
    Mark! I was familiar with across but was not sure of the syntax. I appreciate it! – Ben Blackburn Aug 22 '23 at 16:01

1 Answers1

1

To mutate many columns at once, we need to use across(). The syntax of across() is:

across(.cols, .fns, ..., .names = NULL, .unpack = FALSE)

So, in other words, we list the columns we want to mutate, then the function we want to apply to them.

In the case below, we select the columns by index, and use an anonymous function to always return NA (~ is the beginning of an anonymous function).

df <- matrix(1:9, nrow = 3, ncol = 3) |> as.data.frame() # since you didn't give an example dataframe

df |> mutate(across(1:2, ~ NA))

Output:

  V1 V2 V3
1 NA NA  7
2 NA NA  8
3 NA NA  9
Mark
  • 7,785
  • 2
  • 14
  • 34