【发布时间】:2016-10-29 23:21:37
【问题描述】:
我有一个 .CSV 文件名列表,我想将其内容作为数组mA 读入内存,即我想将一个数组/列表放入mA 的单元格中,以便制作和访问列表列表或数组数组。
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
错误
function (x, ...)
UseMethod("end")
<bytecode: 0x28cb7f8>
<environment: namespace:stats>
function (x, ...)
UseMethod("end")
<bytecode: 0x28cb7f8>
<environment: namespace:stats>
代码 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
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
操作系统:Debian 8.5
R:3.1.1
Linux 内核:4.6 反向移植
硬件:华硕 Zenbook UX303UA
【问题讨论】:
-
a. 数组和列表在 R 中非常不同。您的代码只有列表和 data.frames(从技术上讲是一种列表),而不是数组。 b. Read the canonical answer about lists of data.frames.