我有两个需求,都连接到类似于下面可重现的数据集。我有一个包含 18 个实体的列表,每个实体由 17-19 个 data.frames 的列表组成。可重现的数据集如下(有矩阵而不是 data.frames,但我不认为这有什么区别):
test <- list(list(matrix(10:(50-1), ncol = 10), matrix(60:(100-1), ncol = 10), matrix(110:(150-1), ncol = 10)),
list(matrix(200:(500-1), ncol = 10), matrix(600:(1000-1), ncol = 10), matrix(1100:(1500-1), ncol = 10)))
- 我需要将每个数据帧/矩阵分成两部分(按给定的行数)并保存到新的列表列表
- 其次,我需要从每个列中提取并保存给定的列
data.frame
在列表的列表中。
除了for()
,但我确信这应该是可能的apply()
函数族。
感谢您的阅读
EDIT:
我的预期输出如下:
extractedColumns <- list(list(matrix(10:(50-1), ncol = 10)[, 2], matrix(60:(100-1), ncol = 10)[, 2], matrix(110:(150-1), ncol = 10)[, 2]),
list(matrix(200:(500-1), ncol = 10)[, 2], matrix(600:(1000-1), ncol = 10)[, 2], matrix(1100:(1500-1), ncol = 10)[, 2]))
numToSubset <- 3
substetFrames <- list(list(list(matrix(10:(50-1), ncol = 10)["first length - numToSubset rows", ], matrix(10:(50-1), ncol = 10)["last numToSubset rows", ]),
list(matrix(60:(100-1), ncol = 10)["first length - numToSubset rows", ], matrix(60:(100-1), ncol = 10)["last numToSubset rows", ]),
list(matrix(110:(150-1), ncol = 10)["first length - numToSubset rows", ], matrix(110:(150-1), ncol = 10)["last numToSubset rows", ])),
etc...)
它看起来很混乱,希望你能遵循我的要求。