您很可能需要指定适当的分隔符。
使用文本编辑器打开文本文件并识别分隔符(指定每行中的下一列的字符),之后您将能够:
- 使用具有此分隔符的包装函数,以及分配的其他常用参数*。
- 自己指定参数。
* 参数是您提供给函数的变量,用于指定函数的行为方式。在以下示例中,sep="," 或 header=FALSE 是参数示例。
手动设置参数的方式:
#Columns separated by a ","
read.table("breastdata.txt",header=FALSE,
sep=",")
#Columns separated by a ";"
read.table("breastdata.txt",header=FALSE,
sep=";")
或者,您可以使用自动设置 read.table 的某些参数的包装函数之一来减少您的一些工作:
# Common European format: "," as the decimal point, ";" as the column separator;
read.csv2("breastdata.txt",header=FALSE) # Which would be (almost) equal to read.table("breastdata.txt",header=FALSE,sep=",")
# Common US format: "." as the decimal point, "," as the column separator;
read.csv("breastdata.txt",header=FALSE)
这些基本上调用 read.table 并覆盖默认参数,读取 ?read.table 以查看您可以手动设置的所有参数或查看存在哪些包装器。
编辑:
将数据复制到文本文件并指定空格分隔符是否有效?
dt = read.table("test.txt",header=TRUE,sep=" ")
dt
Diagnosis radius texture perimeter area smoothness compactness concavity concave.points symmetry fractal.dimension
842302 M 17.99 10.38 122.8 1001 0.11840 0.27760 0.3001 0.14710 0.2419 0.07871
842517 M 20.57 17.77 132.9 1326 0.08474 0.07864 0.0869 0.07017 0.1812 0.05667