2

我试图通过以下方式在评估之前动态创建一个表达式:

authors <- c("John1","John2")
exp1 <- "(Author1==%s & Author2==%s)"

我想得到以下字符串:

desired_output <- "(Author1==\"John1\" & Author2!=\"John2\")"

,然后可以使用 eval() 进行评估。

我试过:sprintf(exp1,authors),但这不起作用......解决方案是什么?

4

1 回答 1

4

你可以使用这个:

library(plyr)
splat(sprintf)(c(exp1, authors))

或者没有图书馆:

do.call(sprintf,as.list(c(exp1,authors)))
于 2012-08-03T08:29:07.990 回答