R: How to extract a list from a data frame?

Consider this simple example.

> 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]>

I need to extract the values ​​in col_weird. How can i do this? I see how to do this in Python, but not in R. Expected result:

> 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
+4
source share
5 answers

If you collapse the list column into a row, you can use separatefrom tidyr. I used mapfrom purrr to scroll a column of a list and create a line with toString.

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

In fact, you can use separatedirectly without a part toString, but in the end you will get a “list” as one of the values.

weird_df %>%
     separate(col_weird, into = c("list", "tic", "toc"), convert = TRUE) %>%
     select(-list)

tidyr::extract, . , , .

weird_df %>%
     extract(col_weird, into = c("tic", "toc"), regex = "([[:digit:]]+), ([[:digit:]]+)", convert = TRUE)
+2
weird_df <- data_frame(col1 = c('hello', 'world'),
                   col_weird = list(list(12,23), list(23,24)))

library(dplyr)
weird_df %>%
  dplyr::mutate(tic = unlist(magrittr::extract2(col_weird, 1)),
                toc = unlist(magrittr::extract2(col_weird, 2)),
                col_weird = NULL)

: , col_weird list(NA, NA)

weird_df <- data_frame(col1 = c('hello', 'world', 'again'),
                  col_weird = list(list(12,23), list(23,24), list(NA, NA)))

library(dplyr)
weird_df %>%
 dplyr::mutate(col_weird = matrix(col_weird),
 tic = sapply(col_weird, function(x) magrittr::extract2(x, 1)),
 toc = sapply(col_weird, function(x) magrittr::extract2(x, 2)),
 col_weird = NULL)
+2

R, I():

weird_df <- data.frame(col1 =c('hello', 'world'), 
   col_weird = I(list(list(12,23),list(23,24))))

weird_df
>    col1 col_weird
  1 hello    12, 23
  2 world    23, 24
+2

: purrr/tidyverse/reshape2. unlist "col_weird" map, list, list 'col1', melt 'long', 'L1', "rn" spread ""

library(tidyverse)
library(reshape2)
weird_df$col_weird %>%
     map(unlist) %>% 
     setNames(., weird_df$col1) %>%
     melt %>% 
     group_by(L1) %>%
     mutate(rn = c('tic', 'toc')[row_number()]) %>%
     spread(rn, value) %>%
     left_join(weird_df[-2], ., by = c(col1 = "L1"))
+2

,

> weird_df %>% 
+   rowwise() %>%
+   mutate(tic = col_weird[[1]],
+          tac = ifelse(length(col_weird) == 2, col_weird[[2]], NA)) %>% 
+   select(-col_weird) %>% ungroup()
# A tibble: 3 x 3
   col1   tic   tac
  <chr> <dbl> <dbl>
1 hello    12    23
2 world    23    24
3 again    NA    NA
+2

Source: https://habr.com/ru/post/1680438/


All Articles