Python str.format 的 R 等效项

标签 r string-interpolation

对于下面的代码(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考虑到这些是在全局环境中创建的对象

关于Python str.format 的 R 等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71326182/

相关文章:

r - 如何手动设置条形图中的颜色

r - 从matlab代码到R代码的矩阵

c# - SQL 命令中的字符串插值

javascript - 在某些其他范围对象的 ng-repeat 中使用另一个范围对象值

php - 如何手动插入字符串?

r - 从字符串变量创建虚拟变量

r - 防止 ggplot2 降低 x 轴水平

r - 在 R 中为每个日期的每个 ID 选择第一个正匹配

c# - 是否可以在转义花括号之间使用字符串插值格式复合格式?

Python:使用自定义分隔符格式化字符串