Read a text file with variable number of columns to a list
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.txt234984 10354 41175 932711 4269281693237 13462fc <- 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)
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.