在 R 读取的数据文件中分配列名

Assigning column names in a data file read by R

我正在尝试读取 R 中的网络数据(id 图表)。文件名为 'network.txt',数据如下:

4 0
5 0
6 0
7 0
8 0
9 0
4029 1
4030 1
4031 1
4032 1
4033 1
19088 9040
19089 9040
19090 9040
19091 9040
19092 9040
19093 9040
19094 9040
19095 9040
19096 9040
19097 9040

而且,我正在使用 read.table() 模块阅读它。

data = read.table("network.txt",sep="\t",header=FALSE)
colnames( data ) <- unlist(c('to', 'from'))

Error in `colnames<-`(`*tmp*`, value = c("to", "from")) : 
  'names' attribute [2] must be the same length as the vector [1]

那么,如何分配列名呢?读取原始数据文件有没有错误?

我们只需要

colnames( data ) <- c('to', 'from')

as c('to', 'from') returns a vector and unlist 在这里什么都不做。 unlist 用于输出为 list 或 (data.frame 的情况,这也是一个 list 具有等于 length 又名 columns 的元素)


关于错误,可能是我们使用了错误的 sep 导致了单列,可以通过检查 str(data) 来识别。我会使用 sep=""


除了上面的推荐,我们还可以在read.table

中指定col.names
data <- read.table("network.txt",sep="",header=FALSE, col.names = c("to", "from"))

或使用 data.table 中的 fread(自动选择定界符)

library(data.table)
data <- fread("network.txt", header=FALSE, col.names = c("to", "from"))

您可以在 read.table 函数调用中提供列名,例如:

read.table("network.txt", col.names = c("Col1", "Col2"))

或者,您也可以使用与 names 函数类似的方式进行操作:

test1 <- read.table("Question1.txt")
names(test1) <- c("col1", "col2")