Search code examples
rdplyrtidyverselapplywrangle

mutate specific columns with tidyverse


quick question: Can somebody tell me how I do the following in the dplyr syntax?

p4[,1:5] <- lapply(p4[,1:5] , factor)

Col 1 - 5 were characters and I wanted them to be factors. However, with dplyr I didnt get the job done. My guess was:

df <- df %>% select(1:5) %>% mutate(as_factor)

But that drops all other columns because of the select function. Also, mutate_if is not really helpful here because I have other cols that are characters which I dont want to be changed.

Thank you very much.


Solution

  • One approach to apply a function over a number of columns would be the dplyr::across() function.

    I did not see an example dataset, so have created one of my own.

    library(dplyr)
    data <- data.frame(
      stringsAsFactors = FALSE,
                  col1 = c("apple", "pear"),
                  col2 = c("wood", "fire"),
                  col3 = c("cup", "plate"),
                  col4 = c("pen", "pencil"),
                  col5 = c("money", "coins")
    )
    
    data %>% 
      mutate(across(.cols = 1:5, .fns = factor)) %>% 
      str()
    #> 'data.frame':    2 obs. of  5 variables:
    #>  $ col1: Factor w/ 2 levels "apple","pear": 1 2
    #>  $ col2: Factor w/ 2 levels "fire","wood": 2 1
    #>  $ col3: Factor w/ 2 levels "cup","plate": 1 2
    #>  $ col4: Factor w/ 2 levels "pen","pencil": 1 2
    #>  $ col5: Factor w/ 2 levels "coins","money": 2 1