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
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)
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.
You could simplify the second line by using lapply
instead of sapply
lapply(l, function(x)x[!is.na(x)])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With