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.
2
votes
Why not do header = FALSE then read in the names separately either with readLines or a second read.table command? It's hard to answer fully without seeing exactly what your .txt file looks like
– sayhey69
as 69 has said, try first "header<- read.table(file.txt,header=FALSE,nrow=1) then read.table(file.txt,header=FALSE,skip=200000,nrow=500, col.names=header)"
– Green Demon
Thanks @sayhey69, that was a simple solution that I didn't even think about.
– user13317
2 Answers
3
votes
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".