Masi Masi - 1 month ago 9
R Question

Why this R list of lists fails?

I have a list of .CSV filenames, which contents I want to read to memory as Array

mA
i.e. I want to put an array/list into the cell of
mA
so to make and access a list of lists or array of arrays.

data = read.csv("/home/masi/dataList.csv",header = FALSE,sep = ",")
# P103C1.csv
# P103C2.csv
# ...

i = 1
# http://stackoverflow.com/a/5688126/54964
mA <- vector("list", length(data))
for (d in data)
data[i] = paste("/home/masi/Documents/CSV/", d, sep="")
# /home/masi/Documents/CSV/P103C1.csv
# /home/masi/Documents/CSV/P103C2.csv
# ...

tmp = read.csv(data[i],header = TRUE,sep = ",")

# http://stackoverflow.com/a/10776795/54964
mA[i] = list(tmp[, 1]) # attempt to make a list of lists

i = i + 1
end


Error

function (x, ...)
UseMethod("end")
<bytecode: 0x28cb7f8>
<environment: namespace:stats>
function (x, ...)
UseMethod("end")
<bytecode: 0x28cb7f8>
<environment: namespace:stats>


Code 2



data = scan(file = "/home/masi/dataList.csv", what = character()) # vector, access data[i]
files <- vector("list", length(data))

str(data)

for (i in 1:length(data)) {
tmp = paste0("/home/masi/Documents/CSV/", data[i]) # faster than paste sep=""

print(i)

tmp # does not work

tmp = read.csv(tmp, header = FALSE, sep=","); # no header with other approach
# files[[i]] = list(tmp[, 1])
}

tmp # works if last tmp is commented out


dataList.csv

P103C1.csv
P103C2.csv
P111C1.csv
P111C2.csv
P118C1.csv


P103C1.csv

-0.04,-0.19
-0.045,-0.58
-0.04,-0.465
-0.035,-0.39
-0.01,-0.24


Output where error occur at
i=23


Read 31 items
chr [1:31] "P103C1.csv" "P103C2.csv" "P111C1.csv" "P111C2.csv" ...
...
[1] 21
[1] 22
[1] 23
Error in read.table(file = file, header = header, sep = sep, quote = quote, :
no lines available in input
Calls: read.csv -> read.table
Execution halted


OS: Debian 8.5

R: 3.1.1

Linux kernel: 4.6 backports

Hardware: Asus Zenbook UX303UA

Answer

There three things going wrong here:

So the fix would be

filelst = scan(file = "/home/masi/dataList.csv", what = character()) 
i = 1
# http://stackoverflow.com/a/5688126/54964
mA <- vector("list", length(filelst))
for (file in filelst) {
  tmp = paste0("/home/masi/Documents/CSV/", file) 
  tmp = read.csv(tmp, header = TRUE, sep = ",")
  mA[[i]] = list(tmp[, 1])
  i = i + 1
  }
Comments