数值包括逗号——我如何使这些数字? [英] Number values include comma -- how do I make these numeric?

查看:39
本文介绍了数值包括逗号——我如何使这些数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一整列数字,其中包含千位逗号分隔符.当我尝试用它们创建一个数字列时,任何超过 999 的都变成了 NA.

I have a whole column of numbers that include comma separators at the thousands. When I try to create a numeric column out of them, anything over 999 becomes NA.

我使用了 cbind:

I used cbind:

df <- cbind(df, var2 = as.numeric(as.character(df$var1)))

最后是:

        var1  var2
1   2,518.50    NA
2   2,518.50    NA
3   5,018.50    NA
4   4,018.50    NA
5  10,018.50    NA
6     318.50 318.5
7   2,518.50    NA
8   3,518.50    NA
9   7,518.50    NA
10  1,018.50    NA

有没有办法去掉逗号或告诉 as.numeric 如何处理它们?

Is there a way to strip the commas or tell as.numeric how to handle them?

推荐答案

如果您尝试将新列 var2 添加到 df,您可以使用以下

If you are trying to add a new column var2 to df, you can use the following

  df$var2 <- as.numeric(gsub(",", "", as.character(df$var1)))

这篇关于数值包括逗号——我如何使这些数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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