我试图从我的数据帧tsLyrics
中删除lyrics
列的NULL
值的每一行。我试过了
tsLyrics <- filter(tsLyrics, lyrics == NULL)
但是,我得到以下错误:
Error in `filter()`:
! Problem while computing `..1 = lyrics == NULL`.
x Input `..1` must be of size 338 or 1, not size 0.
我也试着把我的语法改成:
tsLyrics <- filter(tsLyrics, is.null(lyrics))
然而,当我这样做时,我得到一个空数据帧。我应该如何去除这些NULL
?
在适用的情况下,lyrics
列中的每个条目要么是列表,要么是NULL
structure(list(track_name = c("Run (feat. Ed Sheeran) (Taylor’s Version) (From The Vault)",
"The Very First Night (Taylor's Version) (From The Vault)", "All Too Well (10 Minute Version) (Taylor's Version) (From The Vault)",
"State Of Grace (Taylor's Version)", "Red (Taylor's Version)"
), lyrics = list(structure(list(), .Names = character(0), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(), .Names = character(0), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(), .Names = character(0), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(), .Names = character(0), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), NULL)), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))
您可以使用sapply
使用is.null
函数对列进行映射
df <- structure(list(track_name = c("Run (feat. Ed Sheeran) (Taylor’s Version) (From The Vault)",
"The Very First Night (Taylor's Version) (From The Vault)", "All Too Well (10 Minute Version) (Taylor's Version) (From The Vault)",
"State Of Grace (Taylor's Version)", "Red (Taylor's Version)"
), lyrics = list(structure(list(), .Names = character(0), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(), .Names = character(0), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(), .Names = character(0), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(), .Names = character(0), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), NULL)), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))
library(dplyr, warn.conflicts = FALSE)
df
#> # A tibble: 5 × 2
#> track_name lyrics
#> <chr> <list>
#> 1 Run (feat. Ed Sheeran) (Taylor’s Version) (From The Vault) <tibble>
#> 2 The Very First Night (Taylor's Version) (From The Vault) <tibble>
#> 3 All Too Well (10 Minute Version) (Taylor's Version) (From The Vault) <tibble>
#> 4 State Of Grace (Taylor's Version) <tibble>
#> 5 Red (Taylor's Version) <NULL>
df %>%
filter(!sapply(lyrics, is.null))
#> # A tibble: 4 × 2
#> track_name lyrics
#> <chr> <list>
#> 1 Run (feat. Ed Sheeran) (Taylor’s Version) (From The Vault) <tibble>
#> 2 The Very First Night (Taylor's Version) (From The Vault) <tibble>
#> 3 All Too Well (10 Minute Version) (Taylor's Version) (From The Vault) <tibble>
#> 4 State Of Grace (Taylor's Version) <tibble>
在2022-05-04由reprex包(v2.0.1)创建