如何将R中的所有百分比数据转换为十进制? [英] How to convert all percentage data in R to decimal?

查看:77
本文介绍了如何将R中的所有百分比数据转换为十进制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含数值和分类数据的大型数据集.许多列包含 % 数据,即26.2%",因为这些在 R 中不被识别为我希望将它们转换为小数的百分比.

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.

我试过了:

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

然而:

警告信息:强制引入的 NAs

Warning message: NAs introduced by coercion

有人可以帮忙提供正确的方法和/或语法吗?

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

推荐答案

如果你的数据是dataframe,就不能使用sub函数.sub 用于向量.

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

这篇关于如何将R中的所有百分比数据转换为十进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆