R语言 将多个粘贴组合为一个粘贴

vwkv1x7d  于 2023-01-10  发布在  其他
关注(0)|答案(2)|浏览(184)

我使用paste0()创建不同长度的向量,并将它们连接成一个向量:

c(
paste0("A_", 1:16),
paste0("B_", 1:12),
paste0("C_", 3:6),
paste0("D_3_4_C_1_2"),
paste0("E_", rep(1:2, 4), "_", rep(1:4, each=2))
)
[1] "A_1"         "A_2"         "A_3"         "A_4"         "A_5"         "A_6"         "A_7"         "A_8"        
 [9] "A_9"         "A_10"        "A_11"        "A_12"        "A_13"        "A_14"        "A_15"        "A_16"       
[17] "B_1"         "B_2"         "B_3"         "B_4"         "B_5"         "B_6"         "B_7"         "B_8"        
[25] "B_9"         "B_10"        "B_11"        "B_12"        "C_3"         "C_4"         "C_5"         "C_6"        
[33] "D_3_4_C_1_2" "E_1_1"       "E_2_1"       "E_1_2"       "E_2_2"       "E_1_3"       "E_2_3"       "E_1_4"      
[41] "E_2_4"

我的问题是:在需要创建多个矢量的情况下,是否可以将所有paste0组合为一个paste0
预期输出:像paste0(("A_", 1:16), ("B_", 1:12))-〉这样的东西,它不起作用!!!
update:Desired output类似于以下非工作代码:注意:我删除了4个paste0()

c(
paste0("A_", 1:16),
      ("B_", 1:12),
      ("C_", 3:6),
      ("D_3_4_C_1_2"),
      ("E_", rep(1:2, 4), "_", rep(1:4, each=2))
)
txu3uszq

txu3uszq1#

也许这是一个选项,通过创建两个列表,第一个列表是字符串,第二个列表是数字,并将它们与paste0mapply组合在一起,如下所示:

l1 <- list("A_", "B_", "C_", "D_3_4_C_1_2")
l2 <- list(1:16, 1:12, 3:6, 1)
unlist(mapply(paste0, l1, l2))
#>  [1] "A_1"          "A_2"          "A_3"          "A_4"          "A_5"         
#>  [6] "A_6"          "A_7"          "A_8"          "A_9"          "A_10"        
#> [11] "A_11"         "A_12"         "A_13"         "A_14"         "A_15"        
#> [16] "A_16"         "B_1"          "B_2"          "B_3"          "B_4"         
#> [21] "B_5"          "B_6"          "B_7"          "B_8"          "B_9"         
#> [26] "B_10"         "B_11"         "B_12"         "C_3"          "C_4"         
#> [31] "C_5"          "C_6"          "D_3_4_C_1_21"

创建于2023年1月8日,reprex v2.0.2

h7appiyu

h7appiyu2#

string_flatten是您要找的吗?

stringr::str_flatten_comma(c(paste0("A_", 1:16), paste0("B_", 1:12)))

#> [1] "A_1, A_2, A_3, A_4, A_5, A_6, A_7, A_8, A_9, A_10, A_11, A_12, A_13, A_14, A_15, A_16, B_1, B_2, B_3, B_4, B_5, B_6, B_7, B_8, B_9, B_10, B_11, B_12"

stringr::str_flatten(c(paste0("A_", 1:16), paste0("B_", 1:12)))

#> [1] "A_1A_2A_3A_4A_5A_6A_7A_8A_9A_10A_11A_12A_13A_14A_15A_16B_1B_2B_3B_4B_5B_6B_7B_8B_9B_10B_11B_12"

新建编辑

从@Quinten的两个列表和上面评论中的paste0("E_", rep(1:2, 4), "_", rep(1:4, each=2)),我想提供以下两种purrr的方法:

l1 <- list("A_", "B_", "C_", "D_3_4_C_1_2")
l2 <- list(1:16, 1:12, 3:6, 1)

purrr::map2(l1, l2, paste0) |> unlist() |> stringr::str_flatten_comma()
#> [1] "A_1, A_2, A_3, A_4, A_5, A_6, A_7, A_8, A_9, A_10, A_11, A_12, A_13, A_14, A_15, A_16, B_1, B_2, B_3, B_4, B_5, B_6, B_7, B_8, B_9, B_10, B_11, B_12, C_3, C_4, C_5, C_6, D_3_4_C_1_21"
 
# this is from your comment
l3 <- list("E_", rep(1:2, 4))
l32 <- purrr::map2(l3[[1]], l3[[2]], paste0, '_')
l4 <- list(rep(1:4, each=2))

purrr::map2(l32, l4, paste0) |> unlist() |> stringr::str_flatten_comma()
#> [1] "E_1_1, E_1_1, E_1_2, E_1_2, E_1_3, E_1_3, E_1_4, E_1_4, E_2_1, E_2_1, E_2_2, E_2_2, E_2_3, E_2_3, E_2_4, E_2_4, E_1_1, E_1_1, E_1_2, E_1_2, E_1_3, E_1_3, E_1_4, E_1_4, E_2_1, E_2_1, E_2_2, E_2_2, E_2_3, E_2_3, E_2_4, E_2_4, E_1_1, E_1_1, E_1_2, E_1_2, E_1_3, E_1_3, E_1_4, E_1_4, E_2_1, E_2_1, E_2_2, E_2_2, E_2_3, E_2_3, E_2_4, E_2_4, E_1_1, E_1_1, E_1_2, E_1_2, E_1_3, E_1_3, E_1_4, E_1_4, E_2_1, E_2_1, E_2_2, E_2_2, E_2_3, E_2_3, E_2_4, E_2_4"

purrr::map2(l32, l4, paste0) |> unlist()
#>  [1] "E_1_1" "E_1_1" "E_1_2" "E_1_2" "E_1_3" "E_1_3" "E_1_4" "E_1_4" "E_2_1"
#> [10] "E_2_1" "E_2_2" "E_2_2" "E_2_3" "E_2_3" "E_2_4" "E_2_4" "E_1_1" "E_1_1"
#> [19] "E_1_2" "E_1_2" "E_1_3" "E_1_3" "E_1_4" "E_1_4" "E_2_1" "E_2_1" "E_2_2"
#> [28] "E_2_2" "E_2_3" "E_2_3" "E_2_4" "E_2_4" "E_1_1" "E_1_1" "E_1_2" "E_1_2"
#> [37] "E_1_3" "E_1_3" "E_1_4" "E_1_4" "E_2_1" "E_2_1" "E_2_2" "E_2_2" "E_2_3"
#> [46] "E_2_3" "E_2_4" "E_2_4" "E_1_1" "E_1_1" "E_1_2" "E_1_2" "E_1_3" "E_1_3"
#> [55] "E_1_4" "E_1_4" "E_2_1" "E_2_1" "E_2_2" "E_2_2" "E_2_3" "E_2_3" "E_2_4"
#> [64] "E_2_4"

相关问题