Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

Import data into R with an unknown number of columns?

I'm trying to read a text file with different row lengths:

1
1   2
1   2   3
1   2   3   4
1   2   3   4   5
1   2   3   4   5   6
1   2   3   4   5   6   7
1   2   3   4   5   6   7   8

To overcome this problem, I'm using the argument fill=TRUE in read.table, so:

data<-read.table("test",sep="",fill=TRUE)

Unfortunately, to assess the maximum row length, read.table reads only the first 5 lines of the file, and generates an object looking like this:

data
   V1 V2 V3 V4 V5
1   1 NA NA NA NA
2   1  2 NA NA NA
3   1  2  3 NA NA
4   1  2  3  4 NA
5   1  2  3  4  5
6   1  2  3  4  5
7   6 NA NA NA NA
8   1  2  3  4  5
9   6  7 NA NA NA
10  1  2  3  4  5
11  6  7  8 NA NA

Is there a way to force read.table to scroll over the whole file to assess the maximum row length? I know a possible solution would be to provide the column number, like:

data<-read.table("test",sep="",fill=TRUE,col.names=c(1:8))

But since I have a lot of files, I wanted to assess this automatically within R. Any suggestion? :-)


EDIT: the original file doesn't contain progressive numbers, so this is not a solution:

data1<-read.table("test",sep="",fill=TRUE)
data2<-read.table("test",sep="",fill=TRUE,col.names=c(1:max(data1))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is nice function count.fields (see help) which counts number of column per row:

count.fields("test", sep = "")
#[1] 1 2 3 4 5 6 7 8

So, using your second solution:

no_col <- max(count.fields("test", sep = ""))
data <- read.table("test",sep="",fill=TRUE,col.names=1:no_col)
data
#   X1 X2 X3 X4 X5 X6 X7 X8
# 1  1 NA NA NA NA NA NA NA
# 2  1  2 NA NA NA NA NA NA
# 3  1  2  3 NA NA NA NA NA
# 4  1  2  3  4 NA NA NA NA
# 5  1  2  3  4  5 NA NA NA
# 6  1  2  3  4  5  6 NA NA
# 7  1  2  3  4  5  6  7 NA
# 8  1  2  3  4  5  6  7  8

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...