R:如何从数据框中提取列表?

ℕʘʘ*_*ḆḽḘ 5 r dplyr

考虑这个简单的例子

> weird_df <- data_frame(col1 =c('hello', 'world', 'again'),
+                       col_weird = list(list(12,23), list(23,24), NA))
> 
> weird_df
# A tibble: 3 x 2
   col1  col_weird
  <chr>     <list>
1 hello <list [2]>
2 world <list [2]>
3 again  <lgl [1]>
Run Code Online (Sandbox Code Playgroud)

我需要提取col_weird. 我怎样才能做到这一点?我看到如何在 Python 中做到这一点,但在 R 中没有。预期输出是:

> good_df
# A tibble: 3 x 3
   col1   tic   toc
  <chr> <dbl> <dbl>
1 hello    12    23
2 world    23    24
3 again    NA    NA
Run Code Online (Sandbox Code Playgroud)

aos*_*ith 4

如果将列表列折叠成字符串,则可以使用tidyrseparate中的字符串。我使用from purrr循环遍历列表列并使用.maptoString

library(tidyr)
library(purrr)

weird_df %>%
     mutate(col_weird = map(col_weird, toString ) ) %>%
     separate(col_weird, into = c("tic", "toc"), convert = TRUE)

# A tibble: 3 x 3
   col1   tic   toc
* <chr> <int> <int>
1 hello    12    23
2 world    23    24
3 again    NA    NA
Run Code Online (Sandbox Code Playgroud)

实际上,您可以separate直接使用而不使用该toString部分,但最终会得到“list”作为值之一。

weird_df %>%
     separate(col_weird, into = c("list", "tic", "toc"), convert = TRUE) %>%
     select(-list)
Run Code Online (Sandbox Code Playgroud)

这让我想到了tidyr::extract,它与正确的正则表达式配合得很好。但是,如果您的列表列更复杂,则编写正则表达式可能会很痛苦。

weird_df %>%
     extract(col_weird, into = c("tic", "toc"), regex = "([[:digit:]]+), ([[:digit:]]+)", convert = TRUE)
Run Code Online (Sandbox Code Playgroud)