Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

file

list

r

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
like image 784
pms Avatar asked Jan 30 '11 13:01

pms


3 Answers

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)
like image 108
aL3xa Avatar answered Nov 10 '22 23:11

aL3xa


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.

like image 42
pms Avatar answered Nov 11 '22 00:11

pms


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

    lapply(l, function(x)x[!is.na(x)])
like image 1
csgillespie Avatar answered Nov 11 '22 01:11

csgillespie