本文介绍了删除千位分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我导入了一个 Excel 文件并得到了这样的数据框
I imported an Excel file and got a data frame like this
structure(list(A = structure(1:3, .Label = c("1.100", "2.300",
"5.400"), class = "factor"), B = structure(c(3L, 2L, 1L), .Label = c("1.000.000",
"500", "7.800"), class = "factor"), C = structure(1:3, .Label = c("200",
"3.100", "4.500"), class = "factor")), .Names = c("A", "B", "C"
), row.names = c(NA, -3L), class = "data.frame")
我现在想将这些 chars
转换为 numeric
甚至 integer
.但是,点字符 (.
) 不是十进制符号,而是千位分隔符"(德语).
I would now like to convert these chars
to numeric
or even integer
. However, the dot character (.
) is not a decimal sign but a "thousand's separator" (it's German).
如何正确转换数据框?
我试过了:
df2 <- as.data.frame(apply(df1, 2, gsub, pattern = "([0-9])\\.([0-9])", replacement= "\\1\\2"))
df3 <- as.data.frame(data.matrix(df2))
然而,apply
似乎将每一列转换为一个因子列表.我可以阻止 apply
这样做吗?
however, apply
seems to convert each column to a list of factors. Can I maybe prevent apply
from doing so?
推荐答案
我想我刚刚找到了另一个解决方案:
I think I just found another solution:
必须使用stringsAsFactors = FALSE
.
像这样:
df2 <- as.data.frame(apply(df1, 2, gsub, pattern = "([0-9])\\.([0-9])", replacement= "\\1\\2"), stringsAsFactors = FALSE)
df3 <- as.data.frame(data.matrix(df2))
这篇关于删除千位分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!