Sij*_*ari 4 r data-mining data-science
我正在尝试读取 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
Run Code Online (Sandbox Code Playgroud)
而且,我正在使用 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]
Run Code Online (Sandbox Code Playgroud)
那么,如何分配列名呢?读取原始数据文件有没有错误?
小智 5
您可以在read.table函数调用中提供列名,例如:
read.table("network.txt", col.names = c("Col1", "Col2"))
或者,您也可以以与您尝试使用该names功能类似的方式来执行此操作:
test1 <- read.table("Question1.txt")
names(test1) <- c("col1", "col2")
Run Code Online (Sandbox Code Playgroud)