How to use value of variables in expressions in R?
I am relatively new to R. How do I use the value of variables in print and other statements. For example, in Java we can do this by:
System.out.println(" My name is "+ pradeep);
We use the + operator. H开发者_Go百科ow to do this in R?
In R you can do this with paste()
(see ?paste
for more info):
print(paste("My name is ", pradeep, ".", sep = ""))
In general you should prefer Henrik's answer, but note that you can specify strings with sprintf
.
name <- c("Richie", "Pradeep")
sprintf("my name is %s", name)
Assuming
pradeep <- "Pradeep"
Try this:
cat("My name is", pradeep, "\n")
Also the gsubfn package has the ability to add quasi perl-style string interpolation to any command by prefacing the command with fn$
library(gsubfn)
fn$cat("My name is $pradeep\n")
fn$print("My name is $pradeep")
There is also sprintf
and paste
, as mentioned by others.
精彩评论