Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning column names in a data file read by R [duplicate]

I am trying to read a network data (graph of ids) in R. The file is named 'network.txt' and data is as follows:

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

And, I am reading it using read.table() module.

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]

So, how to assign column names? Is there any mistake reading the original data file ?

like image 910
Sijan Bhandari Avatar asked Mar 12 '23 13:03

Sijan Bhandari


1 Answers

You can either provide the column names within the read.table function call, as such:

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

Or, you can also do it in a similar fashion to your attempt with the names function:

test1 <- read.table("Question1.txt")
names(test1) <- c("col1", "col2")
like image 68
David A. Heisler Avatar answered Mar 14 '23 06:03

David A. Heisler