r - 使用 sprintf 时跳过 R 中的 %

标签 r printf

我在 R 中使用“sprintf”函数生成一些数字作为 ggplot 标签。问题是我希望这些数字的百分比如下所示:

sprintf("paste(round(%s*100, 2), '\\%', sep='')", data_plot[1])

如您所见,我使用的是“\”,因此 sprintf 函数不会将其作为特殊字符处理,但我仍然收到以下错误:

Error in sprintf("paste(round(%s*100, 2), '%', sep='')", names(data_plot [1]) : too few arguments

当我用例如“+”替换“%”时,一切正常。我找到了一些关于这个的帖子以及如何编写一个单独的函数来处理这个问题,但我想知道是否有更简单的方法来做到这一点。

注意:这行代码是ggplot代码的一部分,所以必须这样写。

谢谢

最佳答案

您需要在 sprintf 中使用 "%%" 来打印单个 "%"

关于r - 使用 sprintf 时跳过 R 中的 %,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31927195/

相关文章:

从 .r 文件中删除所有评论?

c++ - FPrintf CString 只打印出第一个字母?

r - 使用 mailR 包通过 R 通过 Outlook 发送经过身份验证的邮件

R+dplyr : conditionally swap the elements of two columns

用于子集和汇总的 R data.table 语法

c - fopen、fprintf、fclose 的单行代码?

c - 冒泡排序在 printf (""时不起作用;从代码中删除

C 读取过去的换行符

c - 为什么在 printf 中需要强制转换?

R 数据整理