Reputation: 505
I only need to input a subset of rows from a .txt file. I tried accomplishing this using read.table as follows: read.table(file.txt,header=TRUE,skip=200000,nrow=500)
. However, the resultant data.frame doesn't contain the appropriate header, instead read.table assigns the first row's values (i.e. row 200,000) as the column names. Is there a way to fix this issue? I realize that R will start inputting data from the .txt. file at row 200,000, and with header=TRUE assumes row 200,000 is the header of the data frame. However, I want row 1 (that was skipped) to be the header. Any help would be greatly appreciated.
Upvotes: 2
Views: 580
Reputation: 6197
You could do something like this:
test<-read.table(header=TRUE, text="
a b
1 2
3 4
5 6
7 8
",skip=2,nrow=3)
test1<-read.table(header=TRUE, text="
a b
1 2
3 4
5 6
7 8
",nrows = 1)
colnames(test) <- names(test1)
So first read in the data you want and after that read in the first line of the data to extract the colnames. After that edit the colnames of the dataset you need by the names of the second "dataset".
Upvotes: 3
Reputation: 4208
as 69 has said, try first to get your col heads in a separate file
header<- read.table(file.txt,header=FALSE,nrow=1)
then you can appenhend this col header row to any records u retrieve thereafter.
read.table(file.txt,header=FALSE,skip=200000,nrow=500, col.names=header)"
Upvotes: 0