我有一个 10 x 1 的数据框,其中每个单元格包含 10 个由空格分隔的数字。我将如何拆分此数据帧,以便每个单元格获得一个数字(即 10 x 10 数据帧)?我尝试过拆分和分离功能,但没有成功。谢谢
我的数据:
structure(list(x = structure(1:2, .Label = c("668 1.000 1 14.8876 1 3.474638 3.5887 0.2150 0.348 -0.110 0.992 1 27.7460 1 20.4108 1 6.722 1 112.68000 0",
"686 1.500 1 14.9045 1 3.475565 3.5868 0.3391 -0.064 0.115 1.487 1 27.7420 1 20.4042 1 6.722 1 112.71500 0"
), class = "factor")), class = "data.frame", row.names = c(NA,
-2L))
我们可以尝试separate
从tidyr
:
library(tidyr)
separate(df, 1, paste0("Col", 1:10), sep = "\\s+", convert = TRUE)
使用convert = TRUE
,所有输出列都转换为数字类型。
输出:
Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 Col9 Col10
1 668.000 1.0 1.000 14.8876 1.0000 3.474638 3.5887 0.2150 0.348 -0.110
2 0.992 1.0 27.746 1.0000 20.4108 1.000000 6.7220 1.0000 112.680 0.000
3 686.000 1.5 1.000 14.9045 1.0000 3.475565 3.5868 0.3391 -0.064 0.115
4 1.487 1.0 27.742 1.0000 20.4042 1.000000 6.7220 1.0000 112.715 0.000
数据:
df <- data.frame(x = c("668 1.000 1 14.8876 1 3.474638 3.5887 0.2150 0.348 -0.110",
"0.992 1 27.7460 1 20.4108 1 6.722 1 112.68000 0",
"686 1.500 1 14.9045 1 3.475565 3.5868 0.3391 -0.064 0.115",
"1.487 1 27.7420 1 20.4042 1 6.722 1 112.71500 0"))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句