Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert all % with decimal in R

I have a large data frame where the percentages are written as 10% and not .1. Not all columns are percentage, but quite a few are.

Is there an elegant way to convert all % into decimals? I'm especially concerned where percentages might be greater than 100% and that the rule can be applied to the entire tibble instead of me having to figure out which columns to target.

Example if not clear... this:

tibble(cola = c("hello", "good bye", "hi there"), colb = c("10%", "20%", "100%"), colc = c(53, 67, 89),cold = c("10%", "200%", "50%") )

to this:

tibble(cola = c("hello", "good bye", "hi there"), colb = c(.10, .20, 1.0), colc = c(53, 67, 89),cold = c(.10, 2.0, .5) )

Thanks.

like image 887
seansteele Avatar asked Dec 05 '25 04:12

seansteele


1 Answers

Write an auxiliary function and mutate_if based on its value.

is.percentage <- function(x) any(grepl("%$", x))

df1 %>%
  mutate_if(is.percentage, ~as.numeric(sub("%", "", .))/100)
## A tibble: 3 x 4
#  cola      colb  colc  cold
#  <chr>    <dbl> <dbl> <dbl>
#1 hello      0.1    53   0.1
#2 good bye   0.2    67   2  
#3 hi there   1      89   0.5
like image 167
Rui Barradas Avatar answered Dec 06 '25 20:12

Rui Barradas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!