How does inverted comma work inside sprintf?

85 Views Asked by At

I want an output such as

N(X(1), newx="gen")

I tried to use the following code

try1 <- sprintf("N(X(,%d),newx="gen" )",seq.int(1))

But the above R code gives error. Now how could I do this with sprintf? Any suggestion is extremely appreciated. nb i am quite new to r.

1

There are 1 best solutions below

4
ismirsehregal On

You need to escape the quotation marks:

result <- sprintf("N(X(%d), newx=\"gen\")", seq.int(4))
cat(result, sep = "\n")

Regarding OP's edit:

result <- paste(sprintf("N(X(%d), newx=\"gen\")", seq.int(4)), collapse="+")
cat(result)