我导入了一个 Excel 文件并得到了这样的数据框
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
. 但是,点字符 ( .
) 不是十进制符号,而是“千位分隔符”(它是德语)。
我将如何正确转换数据框?
我试过这个:
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
这样做吗?