我正在尝试使用R中的cat()函数将数据写入文件。我想在输出中写一个“\”字符,但似乎cat()函数将其解释为格式化命令。有关如何在格式化命令中写入此内容的任何想法(例如“\ t \ t \ t \ t \ \ n”)?
答案 0 :(得分:2)
在R中,由于\
是元字符,因此您需要使用\\
在cat()
中打印单个反斜杠。一个是逃脱角色。通过调用cat("\\")
,
以下是一些例子:
> cat("a\nb\tc") ## standard output
a
b c
> cat("a\\nb\\tc") ## prints the control characters in the string
a\nb\tc
> cat("a\\nb\\t\\c") ## prints the control characters in the string,
a\nb\t\c ## and one backslash before "c"
> cat("a\tb\tc\t\\\nd") ## read as "a<tab>b<tab>c<tab>\<newline>d"
a b c \
d
另外,我发现this wikibooks link对于学习使用R进行文本处理非常有用。