How to convert character of percentage into numeric in R

前端 未结 6 1126
一向
一向 2020-11-27 15:29

I run into a problem when converting character of percentage to numeric. E.g. I want to convert \"10%\" into 10%, but

as.numeric(\"10%\")

r

6条回答
  •  無奈伤痛
    2020-11-27 15:48

    Remove the "%", convert to numeric, then divide by 100.

    x <- c("10%","5%")
    as.numeric(sub("%","",x))/100
    # [1] 0.10 0.05
    

提交回复
热议问题