如何在R中gsub('%','\%',...? [英] How to gsub('%', '\%', ... in R?

查看:187
本文介绍了如何在R中gsub('%','\%',...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要导出的乳胶表的单位列带有百分比(%)符号.

I want to export a latex table with a units column that has the percent (%) symbol.

library(xtable) 
foo <- data.frame(units='%', citation = '\\citep{authorYYYYabc}')
print(xtable(foo), sanitize.text.function = function(x) {x})

注意:自从Joris回答以来,以上代码已更改.

在这种情况下,LaTeX将'%'解释为注释.

In this case, the '%' is interpreted as a comment by LaTeX.

我尝试过

gsub('%', '\\%', foo)

返回

[1] "1"

如何将%转换为\%,以便LaTex将其注释掉?

how can I convert the % to \% so that LaTex comments it out?

这个问题有点像以前的问题"可以粘贴('\')吗?";甚至抛光同一张桌子,但我无法弄清楚这种特殊情况.

This question is a little bit like a previous question "can R paste('\')?"; even polishing the same table, but I can't figure out this particular case.

推荐答案

将Joris的答案与注释中的代码相结合:

Combining Joris' answer with the codes in the comment:

library(xtable)
foo <- data.frame(units='%', citation = '\\citep{authorYYYYabc}')
print(xtable(foo), sanitize.text.function = function(x)gsub('%', '\\\\%', x))

这篇关于如何在R中gsub('%','\%',...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆