'Concatenating strings with multiple separators using paste() in R
See the following reproducible example:
require(tidyverse)
set.seed(1)
reprex_df <- data.frame(
var1 = sample(1:10),
var2 = sample(11:20),
var3 = sample(21:30)
)
I am trying to create a new column containing URLs created from concatenating the other variables from each row into a string with "https://www.google.com/search?q=", using the following code:
reprex_df %>% mutate(new_col = c(paste("https://www.google.com/search?q=", var1, var2, var3, sep="+")))
Which results in:
https://www.google.com/search?q=+3+13+30
The problem with this is that it puts a +
between the https://www.google.com/search?q=
and var1
, which is not a valid format for the URL. I need no separator between these strings. Like so:
https://www.google.com/search?q=3+13+30
Can I somehow specify to use a different separator for this part of the conjunction using paste()
, or do I have to take a totally different approach? Any ideas?
Solution 1:[1]
paste0
Perhaps the easiest way is to specify the + signs as arguments withpaste0
rather than usingsep
:root <- "https://www.google.com/search?q=" reprex_df %>% mutate(new_col = paste0(root, var1, "+", var2, "+", var3))
sprintf
sprintf
is another possibility:fmt <- "https://www.google.com/search?q=%d+%d+%d" reprex_df %>% mutate(new_col = sprintf(fmt, var1, var2, var3))
sub
Yet another possibility is to use the code in the question but follow it with code to remove the first +:root <- "https://www.google.com/search?q=" reprex_df %>% mutate(new_col = paste(root, var1, var2, var3, sep="+"), new_col = sub("\\+", "", new_col))
allow extra +
Google ignores the + after the equal sign so another approach is to just allow the extra plus to exist.root <- "https://www.google.com/search?q=" reprex_df %>% mutate(new_col = paste(root, var1, var2, var3, sep="+"))
Solution 2:[2]
You need another paste
reprex_df %>%
mutate(new_col = paste0(
"https://www.google.com/search?q=",
paste(var1, var2, var3, sep = "+")
))
# var1 var2 var3 new_col
#1 3 13 30 https://www.google.com/search?q=3+13+30
#2 4 12 22 https://www.google.com/search?q=4+12+22
#3 5 16 26 https://www.google.com/search?q=5+16+26
# ...
If you don't want to type all the variable names var1
to varn
try purrr::invoke
, thanks to @thelatemail
reprex_df %>%
mutate(new_col = paste0("https://www.google.com/search?q=",
invoke(paste, ., sep = "+")
)
)
Or in base R
url <- "https://www.google.com/search?q=" # optional
transform(reprex_df,
new_col = paste0(url, do.call(paste, c(reprex_df, sep = "+"))))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |