Home > Net >  How to convert character columns with percentage symbols to numeric?
How to convert character columns with percentage symbols to numeric?

Time:04-08

I'd like to convert the character columns with the percentage symbols to plain numeric columns.

Example data

tribble(~`Very little`,  ~Somewhat,   ~`A great deal`, 
          "12.4%",        "11.6%",    "42.1%",
          "1.8%",         "3.4%",     "10%",
          "4.4%",         "41.6%",    "4.2%",
          "1.2%",         "23.7%",    "5.5%"    
)

I was able to find this function with mutate: mutate_all(.tbl, .funs, ...)

But I am not sure how to use this.

CodePudding user response:

You can mess about with some regex to remove the % if you want to but parse_number from readr will do it.

library(tidyverse)
df %>% mutate(across(everything(), parse_number))
  • Related