Vknkmpkt
Vknkmpkt

Reputation: 169

How to sequentially replace substrings in a vector with strings in another vector in R using map function?

I have a vector of three character strings which all contain the substring "X":

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

"X" is a place holder that is either filled by "1", "2", "3" or "a1". Now, what I try to do is to replace "X" with "1", "2", "3" and "a1". Furthermore, I have to add an "a" and a "b" resulting in following vector:

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

I am supposed to do this using dyplr and purrr.

Okay, what I've tried so far reads as follows:

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()

... which results in:

> 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\")"

... with several warnings, all reading:

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

So, while the results are already kind of close to my desired output, there's obviously something wrong with my code/my approach. Can anyone help me out?

Upvotes: 1

Views: 630

Answers (4)

A. Suliman
A. Suliman

Reputation: 13135

You need an additional map inside the last map to iterate over a and b and replace X with 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)))

Upvotes: 1

zx8754
zx8754

Reputation: 56219

Using expand.grid, then paste:

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"

Edit: As suggested by Marcus in the comments:

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

Upvotes: 2

jay.sf
jay.sf

Reputation: 73397

If you are not extremely dependent on dplyr and purrr you could do this in base R using gsub

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"

Upvotes: 2

Aur&#232;le
Aur&#232;le

Reputation: 12839

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"

Created on 2019-04-15 by the reprex package (v0.2.1)

Upvotes: 2

Related Questions