R Equivalent of Python str.format



对于下面的代码(在python中(,R中的等效代码是什么?

print("Sammy is a {}, {}, and {} {}!".format("happy", "smiling", "blue", "shark"))

有多种方法。我们可以使用base R中的sprintf

sprintf("Sammy is a %s, %s and %s %s!", "happy", "smiling", "blue", "shark")
[1] "Sammy is a happy, smiling and blue shark!"

或者考虑到这些是在全局环境中创建的对象,可以使用paste/glue

最新更新