Мы можем unnest
столбцы list
, сгруппированные по 'Level', paste
'элементы в' bounds 'и отформатировать их как sprintf
library(dplyr)
library(tidyr)
example %>%
unnest %>%
group_by(new = Level) %>%
summarise(Level_Bounds = sprintf('%0.2f - (%s)',
first(Level), toString(bounds))) %>%
select(-new)
# A tibble: 2 x 1
# Level_Bounds
# <chr>
#1 0.80 - (2.3, 4.5)
#2 0.90 - (2.7, 5.2)
Если нам нужно два столбцы
library(stringr)
example %>%
unnest %>%
group_by(Level) %>%
summarise(Bounds = str_c("(", toString(bounds), ")"))
# A tibble: 2 x 2
# Level Bounds
# <dbl> <chr>
# 1 0.8 (2.3, 4.5)
#2 0.9 (2.7, 5.2)
Или используя pmap
library(purrr)
example %>%
transmute(Level_bounds = pmap_chr(., ~ paste0(..1, " - (",
toString(..2), ")")))
# A tibble: 2 x 1
# Level_bounds
# <chr>
#1 0.8 - (2.3, 4.5)
#2 0.9 - (2.7, 5.2)
Или используя base R
data.frame(Level = unlist(example$Level),
Bounds = paste0("(", sapply(example$bounds, toString), ")"))