在R中的多个嵌套列表中对复杂数据帧中的列进行子集设置

在R中的多个嵌套列表中对复杂数据帧中的列进行子集设置,r,dataframe,subset,nested-lists,R,Dataframe,Subset,Nested Lists,我试图从包含多个嵌套数据帧的大型列表中提取特定列。以下是我的代码和输出数据: str(ls1) List of 2 $ CAT1:'data.frame': 603 obs. of 2 variables: ..$ M12:'data.frame': 603 obs. of 5 variables: .. ..$ chr : Factor w/ 598 levels "chr1-105554500-105557462",..: 44 45 46 47 48 49

我试图从包含多个嵌套数据帧的大型列表中提取特定列。以下是我的代码和输出数据:

str(ls1)
List of 2
 $ CAT1:'data.frame':   603 obs. of  2 variables:
  ..$ M12:'data.frame': 603 obs. of  5 variables:
  .. ..$ chr        : Factor w/ 598 levels "chr1-105554500-105557462",..: 44 45 46 47 48 49 50 51 52 53 ...
  .. ..$ gene.name  : Factor w/ 551 levels "ENSMUST00000000028-Cdc45",..: 214 184 309 271 267 102 50 315 348 220 ...
  .. ..$ gene.length: int [1:603] 4380 4842 4278 406 357 610 1439 2081 1123 2200 ...
  .. ..$ dir        : Factor w/ 2 levels "-","+": 1 2 1 1 1 2 2 1 2 1 ...
  .. ..$ read.ct    : int [1:603] 307 91 89 84 204 36 10 37 102 77 ...
  ..$ M14:'data.frame': 603 obs. of  5 variables:
  .. ..$ chr        : Factor w/ 596 levels "chr1-105554500-105557462",..: 45 46 47 48 49 50 51 52 53 54 ...
  .. ..$ gene.name  : Factor w/ 549 levels "ENSMUST00000000028-Cdc45",..: 215 184 312 274 270 103 52 318 351 221 ...
  .. ..$ gene.length: int [1:603] 4380 4842 4278 406 357 610 1439 2081 1123 2200 ...
  .. ..$ dir        : Factor w/ 2 levels "-","+": 1 2 1 1 1 2 2 1 2 1 ...
  .. ..$ read.ct    : int [1:603] 370 104 112 89 139 45 12 60 93 70 ...
 $ CAT2:'data.frame':   109 obs. of  2 variables:
  ..$ M12:'data.frame': 109 obs. of  5 variables:
  .. ..$ chr        : Factor w/ 80 levels "chr1-121307307-121312200",..: 6 7 8 1 9 10 2 3 11 12 ...
  .. ..$ gene.name  : Factor w/ 80 levels "ENSMUST00000000365-Mcts1",..: 9 69 71 7 44 58 63 17 32 12 ...
  .. ..$ gene.length: int [1:109] 4205 3229 32462 4894 2048 9952 1334 3698 1787 11235 ...
  .. ..$ dir        : Factor w/ 2 levels "-","+": 2 1 1 1 1 1 1 2 2 2 ...
  .. ..$ read.ct    : int [1:109] 4 2 1 12 18 1 3 1 3 3 ...
  ..$ M14:'data.frame': 109 obs. of  5 variables:
  .. ..$ chr        : Factor w/ 85 levels "chr1-121307307-121312200",..: 7 8 1 9 10 2 11 12 13 3 ...
  .. ..$ gene.name  : Factor w/ 85 levels "ENSMUST00000002291-Paxip1",..: 6 71 4 45 61 65 59 8 9 15 ...
  .. ..$ gene.length: int [1:109] 4205 3229 4894 2048 9952 1334 780 569 11235 1348 ...
  .. ..$ dir        : Factor w/ 2 levels "-","+": 2 1 1 1 1 1 2 2 2 1 ...
  .. ..$ read.ct    : int [1:109] 21 3 6 22 5 2 3 1 1 1 ...
我想要的是能够从每个子列表(即M12、M14)中提取gene.name和read.ct列。我希望它看起来像这样:

List of 2
$ CAT1:'data.frame':  603 obs. of  2 variables:
..$ M12:'data.frame':    603 obs. of  5 variables:
.. ..$ gene.name  : Factor w/ 551 levels "ENSMUST00000000028-Cdc45",..: 214 184 309 271 267 102 50 315 348 220 ...
.. ..$ read.ct    : int [1:603] 307 91 89 84 204 36 10 37 102 77 ...
..$ M14:'data.frame':    603 obs. of  5 variables:
.. ..$ gene.name  : Factor w/ 551 levels "ENSMUST00000000028-Cdc45",..: 214 184 309 271 267 102 50 315 348 220 ...
.. ..$ read.ct    : int [1:603] 307 91 89 84 204 36 10 37 102 77 ...
$ CAT2:'data.frame':  109 obs. of  2 variables:
..$ M12:'data.frame':    109 obs. of  5 variables:
.. ..$ gene.name  : Factor w/ 80 levels "ENSMUST00000000365-Mcts1",..: 9 69 71 7 44 58 63 17 32 12 ...
.. ..$ read.ct    : int [1:109] 4 2 1 12 18 1 3 1 3 3 ...
..$ M14:'data.frame':    109 obs. of  5 variables:
.. ..$ gene.name  : Factor w/ 85 levels "ENSMUST00000002291-Paxip1",..: 6 71 4 45 61 65 59 8 9 15 ...
.. ..$ read.ct    : int [1:109] 21 3 6 22 5 2 3 1 1 1 ...
我应该如何编写代码以获得上述所需的输出?我尝试了以下方法:

ls2 <- lapply(ls1, function(x) {
  y <- x[c(1:2)][c("gene.name", "read.ct")]
  return(y)
})

任何帮助都将不胜感激!谢谢。

第一个数据集的列中似乎嵌套了
data.frame

lapply(ls1, function(x) lapply(x, `[`, c("gene.name", "read.ct")))

似乎
data.frame
嵌套在第一个数据集的列中

lapply(ls1, function(x) lapply(x, `[`, c("gene.name", "read.ct")))

CAT1:'data.frame':603 obs中的变量是什么。共2个变量:变量为
。$M12:'data.frame':
。$M14:'data.frame':
请使用
dput
显示一个小示例,因为结构不清楚
CAT1:'data.frame':603 obs中的变量是什么。共2个变量:
变量为
。$M12:'data.frame':
。$M14:'data.frame':
请使用
dput
显示一个小示例,因为结构不清晰谢谢您的指导!我终于明白了:
ls2谢谢你的指导!我终于明白了:
ls2