How to convert character of percentage into numeric in R

前端 未结 6 1147
一向
一向 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条回答
  •  猫巷女王i
    2020-11-27 15:41

    I wanted to convert an entire column and combined the above answers.

    pct_to_number<- function(x){
      x_replace_pct<-sub("%", "", x)
      x_as_numeric<-as.numeric(x_replace_pct)
      }
    df[['ColumnName']] = pct_to_number(df[['ColumnName']])
    

提交回复
热议问题