Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative function to paste

Tags:

Is there a function that can be an alternative to paste ? I would like to know if something like this exists in R:

> buildString ( "Hi {1}, Have a very nice {2} ! " , c("Tom", "day") )
like image 420
MadSeb Avatar asked Apr 26 '12 20:04

MadSeb


1 Answers

frankc and DWin are right to point you to sprintf().

If for some reason your replacement parts really will be in the form of a vector (i.e. c("Tom", "day")), you can use do.call() to pass them in to sprintf():

string <- "Hi %s, Have a really nice %s!"
vals   <- c("Tom", "day")

do.call(sprintf, as.list(c(string, vals)))
# [1] "Hi Tom, Have a really nice day!"
like image 182
Josh O'Brien Avatar answered Sep 18 '22 14:09

Josh O'Brien