将双向量转换为单个逗号分隔向量
Convert double vector to single comma separated vector
我有一个问题:
example <- tibble(Level = list(0.80,0.90)) %>%
mutate(bounds = list(c(2.3,4.5),
c(2.7,5.2)))
当我制作 gt table 时,
我得到一个 2x2 tibble,看起来像这样:
Level Bounds
0.80 2.3, 4.5
0.90 2.7, 5.1
我想要的是一个 2x2 gt table,它只有一个元素在边界下,在括号中:
Level Bounds
0.80 (2.3,4.5)
0.90 (2.7,5.1)
有办法吗?
我们可以 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), ")"))
一个dplyr
和purrr
的可能性是:
example %>%
transmute(Level_bounds = paste(unlist(Level),
paste0("(", map_chr(bounds, paste, collapse = ","), ")"),
sep = " - "))
Level_bounds
<chr>
1 0.8 - (2.3,4.5)
2 0.9 - (2.7,5.2)
如果实际上需要两列:
example %>%
transmute(Level = unlist(Level),
bounds = paste0("(", map_chr(bounds, paste, collapse = ","), ")"))
Level bounds
<dbl> <chr>
1 0.8 (2.3,4.5)
2 0.9 (2.7,5.2)
我有一个问题:
example <- tibble(Level = list(0.80,0.90)) %>%
mutate(bounds = list(c(2.3,4.5),
c(2.7,5.2)))
当我制作 gt table 时, 我得到一个 2x2 tibble,看起来像这样:
Level Bounds
0.80 2.3, 4.5
0.90 2.7, 5.1
我想要的是一个 2x2 gt table,它只有一个元素在边界下,在括号中:
Level Bounds
0.80 (2.3,4.5)
0.90 (2.7,5.1)
有办法吗?
我们可以 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), ")"))
一个dplyr
和purrr
的可能性是:
example %>%
transmute(Level_bounds = paste(unlist(Level),
paste0("(", map_chr(bounds, paste, collapse = ","), ")"),
sep = " - "))
Level_bounds
<chr>
1 0.8 - (2.3,4.5)
2 0.9 - (2.7,5.2)
如果实际上需要两列:
example %>%
transmute(Level = unlist(Level),
bounds = paste0("(", map_chr(bounds, paste, collapse = ","), ")"))
Level bounds
<dbl> <chr>
1 0.8 (2.3,4.5)
2 0.9 (2.7,5.2)