pms
pms

Reputation: 4626

Read a text file with variable number of columns to a list

I have a file like this:

mylist.txt
234984  10354  41175 932711 426928
1693237   13462

Each line of this file has different number of elements, minimum of 1 element per line. I would like to read it into a list like this:

> print(head(mylist,2))
[[1]]
[1] 234984  10354  41175 932711 426928

[[2]]
[1] 1693237   13462

Upvotes: 16

Views: 11633

Answers (3)

csgillespie
csgillespie

Reputation: 60522

You could simplify the second line by using lapply instead of sapply

    lapply(l, function(x)x[!is.na(x)])

Upvotes: 1

pms
pms

Reputation: 4626

A possible answer is to first read a list filled with NAs and then removing them like this:

l<-as.list( as.data.frame( t(read.table("mylist.txt",fill=TRUE,col.names=1:max(count.fields("mylist.txt"))))) )
l<-lapply(l, function(x) x[!is.na(x)] )

I wonder if there is a simpler way of doing it.

Upvotes: 2

aL3xa
aL3xa

Reputation: 36120

Assuming that space is delimiter:

fc <- file("mylist.txt")
mylist <- strsplit(readLines(fc), " ")
close(fc)

EDIT:

If the values are delimited by several spaces (an/or in unconsistent way), you can match delimiter with regular expression:

mylist.txt
234984   10354   41175 932711      426928
1693237               13462

fc <- file("mylist.txt")
mylist <- strsplit(readLines(fc), " +")
close(fc)

EDIT #2

And since strsplit returns strings, you need to convert your data to numeric (that's an easy one):

mylist <- lapply(mylist, as.numeric)

Upvotes: 20

Related Questions