I wish to insert a defined variable into a string in R, where the variable will be inserted in multiple places.
I have seen the sprintf may be of use.
Example of desired input:
a <- "tree"
b <- sprintf("The %s is large but %s is small. %s", a)
Ideal output would return
"The tree is large but tree is small. tree"
I understand that I can use the function like this:
b <- sprintf("The %s is large but %s is small. %s",a,a,a)
However for my actual work I would require the insert 10+ times, so i'm looking for a cleaner/simpler solution.
Would gsub be a better solution?
My exact question has been answered here however it is for the language Go:
gsub('%s' ,a, 'The %s is large but %s is small. %s')?