从采样列表中删除空的标题列表

rocknRrr

我为重采样列表应用了RNN,五分之三的列表均未产生预测,因此想删除空列表。

# A tibble: 5 x 3
  splits          id     predict          
  <list>          <chr>  <list>           
1 <split [24/12]> Slice1 <tibble [48 × 3]>
2 <split [24/12]> Slice2 <tibble [48 × 3]>
3 <split [24/12]> Slice3 <lgl [1]>        
4 <split [24/12]> Slice4 <lgl [1]>        
5 <split [24/12]> Slice5 <lgl [1]>  

因此,尝试使用discard()或Filter()删除列表3、4、5,但是我无法从小标题中将列表子集化。

> discard(sample_predictions, ~nrow(.) == 0)
Error: Predicate functions must return a single `TRUE` or `FALSE`, not a logical vector of length 0
Backtrace:
 1. purrr::discard(sample_predictions, ~nrow(.) == 0)
 2. purrr:::probe(.x, .p, ...)
 3. purrr::map_lgl(.x, .p, ...)
 4. purrr:::.f(.x[[i]], ...)

> Filter(function(x) dim[x]>0,sample_predictions )
Error in dim[x] : object of type 'builtin' is not subsettable
阿克伦

在这里,我们可以使用map根据显示的数据,某些list元素是length1而不是的逻辑向量tibble一种选择是filter通过遍历listwithmap并检查它是否是小标题(is_tibble

library(dplyr)
library(purrr)
sample_predictions %>% 
          filter(map_lgl(predict, is_tibble))

或使用 base R

sample_predictions[sapply(sample_predictions$predict, Negate(is.logical)),]

数据

sample_predictions <- tibble(predict = list(tibble(col1 = 1:5), 
        tibble(col1 = 1:3), TRUE))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章