R: Error converting character type to numeric type when using select_if() and gsub()

I have a dataset of multiple types. It was created in an Excel spreadsheet so some numbers contain commas (e.g. 1,346 instead of 1346). Hence, making them of type character instead of numeric.

Here's what I attempted to make the conversion:

df[-2] %>% 
  select_if(is.character) %>% 
  as.numeric(gsub(",", "", df))

I am excluding the second column from the selection as it is a valid character type for my analysis.

The error I am getting is:

Error in df[-2] %>% select_if(is.character) %>% as.numeric(gsub(",",  : 
  'list' object cannot be coerced to type 'double'

How could I make this work?

Read more here: https://stackoverflow.com/questions/67007857/r-error-converting-character-type-to-numeric-type-when-using-select-if-and-gs

Content Attribution

This content was originally published by Joehat at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: