这是一个微不足道的问题,但我被困住了。如何根据数据框的长度过滤数据框列表?该列表是嵌套的 - 这意味着有不同长度的数据框的列表的列表。以下是一个示例。我想要过滤或子集化列表,只包括那些对象长度小于n,比如3。
这里是一个示例和我的当前方法。
library(tidyverse)
# list of list with arbitrary lengths
star.wars_ls <- list(starwars[1:5],
list(starwars[1:8], starwars[4:6]),
starwars[1:2],
list(starwars[1:7], starwars[2:6]),
starwars[1:3])
# I want to filter the list by dataframes that are 3 variables long (i.e. length(df == 3).
# Here is my attempt, I'm stuck at how to obtain
# the number of varibles in each dataframe and then filter by it.
map(star.wars_ls, function(x){
map(x, function(x){ ## Incorrectly returns 20 for all
length(y)
})
})
n
和(b)定义y
。如果能看到期望的结果会更好。比如,如果n
是3,您是否希望得到list(NULL, list(NULL, starwars[4:6]), list(NULL, NULL), starwars[1:3])
?或者其他什么结果? - Gregor Thomas