简体   繁体   English

尝试将不同长度的多个文件读入R中的数组

[英]Trying to read multiple files of different lengths into an array in R

I feel like this is a fairly simple question. 我觉得这是一个相当简单的问题。 I am dealing with 217 different "text" (they're actually .edf files I've converted to text) files, each of which containing a 9 column table of attributes. 我正在处理217个不同的“文本”(它们实际上是我已转换为文本的.edf文件)文件,每个文件都包含一个9列的属性表。 The data is coming from sensors that were dropped to the ocean floor, so each has a different number of rows. 数据来自掉落到海底的传感器,因此每个传感器具有不同的行数。 I would like to combine all of these files into one array in R of dimension 9399x9x217. 我想将所有这些文件组合成尺寸为9399x9x217的R中的一个数组。 (9399 is the number of rows in the largest file). (9399是最大文件中的行数)。 Currently I'm using this code: 目前,我正在使用以下代码:

omgdatlist <- list.files(pattern ="*.edf")
named.list <- array(0, dim = c(9399, 9, 217))
for (i in 1:217)
{
named.list[i] <- matrix(assign(omgdatlist[i], read.table(omgdatlist[i], header = FALSE, skip = 51, col.names = c("Time(sec)", "Frame", "Depth(m)", "Temperature(C)", "Conductivity", "Salinity", "Sound Velocity", "Density", "Status"))), nrow = 9399, ncol = 9)
}

However, it's returning a list of 18356247. Is using an array even the most efficient way of doing this? 但是,它返回的列表为18356247。使用数组甚至是执行此操作的最有效方法吗?

Because you want to access the ith 2D cell of your array, you need to replace named.list[i] by named.list[,,i] . 因为要访问数组的第2个2D单元,所以需要将named.list[i]替换为named.list[,,i]

Here is a minimum working example: 这是一个最小的工作示例:

named.list <- array(0, dim = c(3, 1, 2))
for (i in 1:dim(named.list)[3])
{
  named.list[,,i] <- array(i, dim = c(3, 1))
}
named.list 

which returns 哪个返回

> named.list
, , 1

     [,1]
[1,]    1
[2,]    1
[3,]    1

, , 2

     [,1]
[1,]    2
[2,]    2
[3,]    2

As for efficiency, you might want to look at ?sapply and ?apply . 至于效率,您可能想看看?sapply?apply

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM