(R) 如何删除指定列中所有具有 NULL 值的行?
(R) How to remove all rows that have a NULL value in a specified column?
我正在尝试从我的数据框 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
s?
如果适用,lyrics
列中的每个条目要么是列表,要么是 NULL
Data Example
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>
由 reprex package (v2.0.1)
创建于 2022-05-04
我正在尝试从我的数据框 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
s?
如果适用,lyrics
列中的每个条目要么是列表,要么是 NULL
Data Example
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>
由 reprex package (v2.0.1)
创建于 2022-05-04