Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据行数在data.frames列表中选择data.frames,使用R_R_List - Fatal编程技术网

根据行数在data.frames列表中选择data.frames,使用R

根据行数在data.frames列表中选择data.frames,使用R,r,list,R,List,以下是我列表的前两个data.frames: lizlist <- list(structure(c(309, 356, 378, 403, 391, 423, 151, 440, 483, 493, 504, 384, 525, 523, 522, 523, 1801, 2078, 2535, 2650, 2765, 3247, 3764, 3826, 4490, 4974, 5106, 5279, 5764, 6372, 6879, 6983, 1792, 2070, 2528,

以下是我列表的前两个data.frames:

lizlist <- list(structure(c(309, 356, 378, 403, 391, 423, 151, 440, 483, 
493, 504, 384, 525, 523, 522, 523, 1801, 2078, 2535, 2650, 2765, 
3247, 3764, 3826, 4490, 4974, 5106, 5279, 5764, 6372, 6879, 6983, 
1792, 2070, 2528, 2644, 2755, 3241, 3758, 3819, 4481, 4966, 5097, 
5273, 5756, 6362, 6869, 6975, 1808, 2085, 2542, 2658, 2772, 3256, 
3770, 3836, 4500, 4981, 5116, 5288, 5776, 6384, 6891, 6995), .Dim = c(16L, 
4L)), structure(c(388, 409, 460, 471, 493, 514, 537, 590, 607, 
609, 630, 629, 641, 634, 1814, 2089, 2546, 2660, 2774, 3255, 
3833, 4497, 4981, 5115, 5775, 6386, 6896, 7001, 1807, 2080, 2538, 
2653, 2766, 3245, 3825, 4489, 4973, 5106, 5765, 6374, 6885, 6991, 
1821, 2096, 2555, 2668, 2781, 3268, 3844, 4507, 4988, 5125, 5784, 
6397, 6909, 7012), .Dim = c(14L, 4L)))
再次感谢你的帮助

你想要的是

lizlist[sapply(lizlist, nrow)==14]
请记住,这将返回满足条件的所有data.frames的
列表(这通常是件好事)。如果您知道列表将只包含一个这样的data.frame,则可以使用
[[[
这两个

尝试一下(在我收到帮助确定第一次尝试失败原因的错误消息后)

lapply(lizlist,function(x) x[which(nrow(x)==14),])
您的尝试成功地访问了
lizlist
中所需的元素,但随后返回了第一行,因为which()函数在该元素中返回TRUE,并将其转换为数字1

要回答您对数据帧索引的请求,请尝试:

> which( sapply(lizlist,function(x) nrow(x)==14) )
[1] 2

很好,可以保留所选列表的索引吗?在上面的示例中,列表索引为2。
sapply
比我的lapply尝试要求取消列出结果要好。可以通过
访问索引(sapply(lizlist,nrow)==14)
。如果你真的想保留索引,我想你可以做
lappy(lizlist,function(x)If(nrow(x)==14)x)
> which( sapply(lizlist,function(x) nrow(x)==14) )
[1] 2