How to convert all percentage data in R to decimal? [duplicate]

喜你入骨 提交于 2021-01-28 15:37:39

问题


I have a large data set containing both numerical and categorical data. A number of columns contain % data i.e. "26.2%", as these are not recognised in R as percentages I wish to convert them to decimals.

I have tried:

data2 <- as.numeric(sub("%", "",data,fixed=TRUE))/100

However:

Warning message: NAs introduced by coercion

Can someone please help with the correct approach and/or syntax?


回答1:


If your data is a dataframe, you can not use the sub function. sub is for vectors.

Try using the same function but column by column e.g.

column1 <- as.numeric(sub("%", "",data$column1,fixed=TRUE))/100



回答2:


You could try:

library(dplyr)
df %>% mutate_each(funs(as.numeric(gsub("%", "", ., fixed = TRUE))/100))



回答3:


To apply to all columns you can combine the code provided by the other users with an apply statement. For example,

apply(d,2, function(x){
 as.numeric(sub("%", "", x, fixed=TRUE))/100}

where d is your dataframe



来源:https://stackoverflow.com/questions/29312622/how-to-convert-all-percentage-data-in-r-to-decimal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!