R 按观察数过滤嵌套数据帧

R 按观察数过滤嵌套数据帧,r,dataframe,nested,dplyr,tidyverse,R,Dataframe,Nested,Dplyr,Tidyverse,以下来自: 我想嵌套在多个列上,然后根据嵌套到该行中的项目数筛选出行。比如说, df <- tibble( a = sample(x = c(rep(c('x','y'),4), 'w', 'z')), b = sample(c(1:10)), c = sample(c(91:100)) ) df您可以使用data列上的map/map\u int返回每个嵌套TIBLE中的nrow,并基于它构造过滤条件: df %>% nest(-a) %>%

以下来自:

我想嵌套在多个列上,然后根据嵌套到该行中的项目数筛选出行。比如说,

df <- tibble(
  a = sample(x = c(rep(c('x','y'),4), 'w', 'z')),
  b = sample(c(1:10)),
  c = sample(c(91:100))
)

df您可以使用
data
列上的
map/map\u int
返回每个嵌套TIBLE中的
nrow
,并基于它构造过滤条件:

df %>% 
    nest(-a) %>% 
    filter(map_int(data, nrow) == 1)
#   filter(map(data, nrow) == 1)        works as well

# A tibble: 2 x 2
#      a             data
#  <chr>           <list>
#1     w <tibble [1 x 2]>
#2     z <tibble [1 x 2]>
df%>%
嵌套(-a)%>%
过滤器(映射int(数据,nrow)==1)
#过滤器(map(data,nrow)==1)也可以工作
#一个tibble:2x2
#数据
#             
#1瓦
#2 z
df %>% 
    nest(-a) %>% 
    filter(map_int(data, nrow) == 1)
#   filter(map(data, nrow) == 1)        works as well

# A tibble: 2 x 2
#      a             data
#  <chr>           <list>
#1     w <tibble [1 x 2]>
#2     z <tibble [1 x 2]>