问题
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