[英]Subsetting columns within complex data frames within multiple nested lists in R
I'm trying to extract a specific column from a large list with multiple nested dataframes.我正在尝试从具有多个嵌套数据框的大列表中提取特定列。 Here's my code and output data:这是我的代码和输出数据:
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 ...
What I want is to be able to extract the gene.name and read.ct columns from each sublist (ie M12, M14).我想要的是能够从每个子列表(即M12、M14)中提取gene.name 和read.ct 列。 I'd like it to look like this:我希望它看起来像这样:
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 ...
How should I write the code to get the above desired output?我应该如何编写代码以获得上述所需的输出? I tried the following:我尝试了以下方法:
ls2 <- lapply(ls1, function(x) {
y <- x[c(1:2)][c("gene.name", "read.ct")]
return(y)
})
but I get the error:但我收到错误:
Error in `[.data.frame`(x[c(1:2)], c("gene.name", "read.ct")) :
undefined columns selected
Any help would be appreciated!任何帮助,将不胜感激! Thank you.谢谢你。
似乎data.frame
嵌套在第一个数据集的列中
lapply(ls1, function(x) lapply(x, `[`, c("gene.name", "read.ct")))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.