William_Fitchen@oxy.com
2001-Dec-19 21:56 UTC
[R] Interpolating variables into quoted strings
Hello I am new to R and am coming from a Perl background. I have had trouble figuring out from the documentation how to interpolate a variable into a quoted string (if it's possible). This seems to be necessary when writing a script that must print out strings (for example plot legends) whose content is calculated during the execution of the script. In perl I could write: $name = "John"; print STDOUT "Hi $name, how's it going"; which would output the following: Hi John, how's it going? Is there a function that allows a variable containing a character string or numeric to be interpolated into a quoted character string? Any help would be greatly appreciated! ------------------------ William M. Fitchen Occidental Permian Ltd. 580 Westlake Park Blvd. Westlake 2, Rm. 431A Houston, TX 77079 281-552-1225 (voice) william_fitchen at oxy.com ------------------------ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
William_Fitchen at oxy.com writes:> I am new to R and am coming from a Perl background. I have had trouble > figuring out from the documentation how to interpolate a variable into a > quoted string (if it's possible). This seems to be necessary when writing a > script that must print out strings (for example plot legends) whose content > is calculated during the execution of the script. > > In perl I could write: > > $name = "John"; > print STDOUT "Hi $name, how's it going"; > > which would output the following: > > Hi John, how's it going? > > Is there a function that allows a variable containing a character string or > numeric to be interpolated into a quoted character string? Any help would > be greatly appreciated!Generally paste() or cat() is used for this: name <- "John" cat("Hi ", name, ", how's it going\n", sep="") or out <- paste("Hi ", name, ", how's it going", sep="") print(out) You could also try stuff like sub("NAME", name, "Hi, NAME, how's it going") -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Wed, Dec 19, 2001 at 03:56:30PM -0600, William_Fitchen at oxy.com wrote:> In perl I could write: > > $name = "John"; > print STDOUT "Hi $name, how's it going"; > > which would output the following: > > Hi John, how's it going? > > Is there a function that allows a variable containing a character string or > numeric to be interpolated into a quoted character string? Any help would > be greatly appreciated!name <- "John" cat(paste("Hi", name, "how's it going\n")) You might want to look at the "An Introduction to R" manual shipped with R, it covers paste() in the "Character vectors" section. Dirk -- Good judgment comes from experience; experience comes from bad judgment. -- F. Brooks -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._