如何使用map函数将向量中的子字符串顺序替换为R中另一个向量中的字符串?

时间:2019-04-15 11:48:50

标签: r string loops replace purrr

我有一个包含三个字符串的向量,这些字符串都包含子字符串“ X”:

v<- c("kpX_43", "kpX_10", "kpX_11")

“ X”是一个占位符,由“ 1”,“ 2”,“ 3”或“ a1”填充。现在,我要做的是用“ 1”,“ 2”,“ 3”和“ a1”替换“ X”。此外,我必须添加一个“ a”和一个“ b”,得到以下向量:

> v_new
[1] "kp1_43a" "kp1_10a" "kp1_11a" "kp2_43a" "kp2_10a" "kp2_11a" "kp3_43a" "kp3_10a" "kp3_11a" "kp1_43b" "kpa1_10a" "kpa1_11a" "kpa1_43b" "kp1_10b" "kp1_11b" "kp2_43b" "kp2_10b" "kp2_11b" "kp3_43b" "kp3_10b" "kp3_11b" "kpa1_10b" "kpa1_11b" "kpa1_43b"

我应该使用dyplr和purrr进行此操作。

好的,到目前为止我尝试的内容如下:

strings<- as.matrix(c(seq(1:3), "a1"))

v_new <- v %>% 
  map(., ~str_c(., letters[seq(from = 1, to = 2)])) %>% 
  map(function(x) {
    str_replace_all(., "X" , strings)}) %>%
  unlist()

...结果为:

> v_new
 [1] "c(\"kp1_43a\", \"kp1_43b\")"   "c(\"kp2_10a\", \"kp2_10b\")"   "c(\"kp3_11a\", \"kp3_11b\")"   "c(\"kpa1_43a\", \"kpa1_43b\")"
 [5] "c(\"kp1_43a\", \"kp1_43b\")"   "c(\"kp2_10a\", \"kp2_10b\")"   "c(\"kp3_11a\", \"kp3_11b\")"   "c(\"kpa1_43a\", \"kpa1_43b\")"
 [9] "c(\"kp1_43a\", \"kp1_43b\")"   "c(\"kp2_10a\", \"kp2_10b\")"   "c(\"kp3_11a\", \"kp3_11b\")"   "c(\"kpa1_43a\", \"kpa1_43b\")"

...有几个警告,全部显示为:

Warning messages:
1: In stri_replace_all_regex(string, pattern, fix_replacement(replacement),  :argument is not an atomic vector; coercing

因此,虽然结果已经接近我期望的输出,但是我的代码/方法显然存在问题。有人可以帮我吗?

4 个答案:

答案 0 :(得分:2)

library(purrr)

v <- c("kpX_43", "kpX_10", "kpX_11")
strings <- c(1:3, "a1")
suffixes <- c("a", "b")

cross(tibble::lst(v, strings, suffixes)) %>% 
  map_chr(~ paste0(sub("X", .$strings, .$v), .$suffixes))
#>  [1] "kp1_43a"  "kp1_10a"  "kp1_11a"  "kp2_43a"  "kp2_10a"  "kp2_11a" 
#>  [7] "kp3_43a"  "kp3_10a"  "kp3_11a"  "kpa1_43a" "kpa1_10a" "kpa1_11a"
#> [13] "kp1_43b"  "kp1_10b"  "kp1_11b"  "kp2_43b"  "kp2_10b"  "kp2_11b" 
#> [19] "kp3_43b"  "kp3_10b"  "kp3_11b"  "kpa1_43b" "kpa1_10b" "kpa1_11b"

reprex package(v0.2.1)于2019-04-15创建

答案 1 :(得分:2)

使用 expand.grid ,然后使用粘贴

apply(expand.grid("kp",
                  c("1", "2", "3", "a1"),
                  "_",
                  c(43, 10, 11),
                  c("a", "b")), 1, paste, collapse = "")

#  [1] "kp1_43a"  "kp2_43a"  "kp3_43a"  "kpa1_43a" "kp1_10a"  "kp2_10a" 
#  [7] "kp3_10a"  "kpa1_10a" "kp1_11a"  "kp2_11a"  "kp3_11a"  "kpa1_11a"
# [13] "kp1_43b"  "kp2_43b"  "kp3_43b"  "kpa1_43b" "kp1_10b"  "kp2_10b" 
# [19] "kp3_10b"  "kpa1_10b" "kp1_11b"  "kp2_11b"  "kp3_11b"  "kpa1_11b"

编辑:如Marcus在评论中所建议:

do.call(paste0, expand.grid("kp",
                            c("1", "2", "3", "a1"),
                            "_",
                            c(43, 10, 11),
                            c("a", "b")))

答案 2 :(得分:2)

如果您不太依赖dplyr和purrr,则可以使用gsub

在base R中执行此操作
as.vector(sapply(strings, function(x) 
  paste0(gsub("X", x, v), rep(letters[1:2], 3))))
# [1] "kp1_43a"  "kp1_10b"  "kp1_11a"  "kp1_43b"  "kp1_10a" 
# [6] "kp1_11b"  "kp2_43a"  "kp2_10b"  "kp2_11a"  "kp2_43b" 
# [11] "kp2_10a"  "kp2_11b"  "kp3_43a"  "kp3_10b"  "kp3_11a" 
# [16] "kp3_43b"  "kp3_10a"  "kp3_11b"  "kpa1_43a" "kpa1_10b"
# [21] "kpa1_11a" "kpa1_43b" "kpa1_10a" "kpa1_11b"

答案 3 :(得分:1)

您需要在最后一个映射内添加一个map,以迭代ab并将X替换为 strings

library(purrr)
v %>% map(., ~str_c(., letters[seq(from = 1, to = 2)])) %>%
      map(. %>% map(.,~str_replace_all(.,'X',strings))) %>% unlist()
      #OR map(~map(.,~str_replace_all(.,'X',strings)))