[英]Remove empty tibble lists from sampling lists
我將 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,但我未能從tibbles 中對列表進行子集化。
> 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
。 一個選項是通過使用map
循環遍歷list
來filter
並檢查它是否是is_tibble
( 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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.