Search code examples
rtidyversepurrr

Is it possible to use dplyr::select to select columns and update at the same time?


Sample dataset:

a = data.frame(a=c(1,2,3,4),b = c('1','2','3','4'),c=c('6','6','6','6'))

What I want to do is:

  1. select the b and c from dataframe a;
  2. change the class of b and c from character to numeric

The code that I tried:

a %>% select(b,c) %<>% as.numeric

The error appeared:

Error in a %>% select(b, c) %<>% as.numeric : 
  'list' object cannot be coerced to type 'double'
Warning: cannot xtfrm data frames

Solution

  • We may use mutate with across as as.numeric expects a vector as input and not a data.frame or list

    library(magrittr)
    a %<>%    
         mutate(across(where(is.character), as.numeric))
    

    -output

    > str(a)
    'data.frame':   4 obs. of  3 variables:
     $ a: num  1 2 3 4
     $ b: num  1 2 3 4
     $ c: num  6 6 6 6
    

    Or use data.table methods

    library(data.table)
     setDT(a)[, (2:3) := lapply(.SD, as.numeric), .SDcols = is.character ]
    

    -output

    > str(a)
    Classes ‘data.table’ and 'data.frame':  4 obs. of  3 variables:
     $ a: num  1 2 3 4
     $ b: num  1 2 3 4
     $ c: num  6 6 6 6
    

    Or another option is to automatically convert to its type based on the value with type.convert

    a %<>%
        type.convert(as.is = TRUE)