我为重采样列表应用了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
元素是length
1而不是的逻辑向量tibble
。一种选择是filter
通过遍历list
withmap
并检查它是否是小标题(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] 删除。
我来说两句