rangeelo
rangeelo

Reputation: 118

replacing variable in a string(url) with a list of values in R

I'm trying to generate URLs that follow a standard pattern: abc.xvz/var/

I have a list of values, which I want to insert in the place of 'var'

my code looks something like:

library(gsubfn)

t <- function(l){
u <- "abc.xyz/var/"
gsubfn(pattern = 'var',x = u, replacement = l)
}

test <- do.call(t, list)

however, I get the unused arguments error, and the result makes use of only the first item inside list

What am I doing wrong?

Upvotes: 0

Views: 31

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521259

Use lapply:

u <- "abc.xyz/var/"
urls <- lapply(l, function(x) gsub("var", x, u))

Upvotes: 1

akrun
akrun

Reputation: 887118

We can use sapply with t to get a vector output

sapply(lst, t)

where lst is the list of urls.

Upvotes: 0

Related Questions