Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何在R中显示带引号的文本?_String_R_Quotes - Fatal编程技术网

String 如何在R中显示带引号的文本?

String 如何在R中显示带引号的文本?,string,r,quotes,String,R,Quotes,我已经开始学习R,并尝试创建向量,如下所示: c(""check"") 我需要输出为:“检查”。但是我有语法错误。如何在创建向量时转义引号?使用反斜杠: x <- "say \"Hello!\"" 正如@juba所提到的,一种方法是直接转义引号 另一种方法是在包含双引号的字符表达式周围使用单引号 > x <- 'say "Hello!"' > x [1] "say \"Hello!\"" > cat(x) say "Hello!" >x [1] “说\”你好\

我已经开始学习R,并尝试创建向量,如下所示:

c(""check"")
我需要输出为:“检查”。但是我有语法错误。如何在创建向量时转义引号?

使用反斜杠:

x <- "say \"Hello!\""

正如@juba所提到的,一种方法是直接转义引号

另一种方法是在包含双引号的字符表达式周围使用单引号

> x <- 'say "Hello!"'
> x
[1] "say \"Hello!\""
> cat(x)
say "Hello!"
>x
[1] “说\”你好\""
>猫(x)
说“你好!”

其他答案很好地展示了在创建向量时如何处理字符串中的双引号,这确实是您在问题中最后问的问题。但考虑到您也提到了显示和输出,您可能需要记住
dQuote
。如果您想用双引号围绕字符向量的每个元素,特别是如果您不需要或不希望将引号存储在实际的字符向量本身中,那么它非常有用

# default is to use "fancy quotes"
text <- c("check")
message(dQuote(text))
## “check”

# switch to straight quotes by setting an option
options(useFancyQuotes = FALSE)
message(dQuote(text))
## "check"

# assign result to create a vector of quoted character strings
text.quoted <- dQuote(text)
message(text.quoted)
## "check"
#默认为使用“花式引号”

文本我以前见过“如果你不构建向量,你就不需要
c
”这句话,我知道你不需要它,但我只是好奇:它会痛吗?不,它不会痛,因为结果是一样的。我想如果你只做
x,那么如何将这个双引号字符串赋给变量呢?需要注意的一点是dQuote和sQuote可能会使用“smart”或“fancy”引号来代替普通的ASCII引号字符。例如,如果您正在写入CSV文件,则这可能不是您想要的。此问题的公认答案在哪里?
# default is to use "fancy quotes"
text <- c("check")
message(dQuote(text))
## “check”

# switch to straight quotes by setting an option
options(useFancyQuotes = FALSE)
message(dQuote(text))
## "check"

# assign result to create a vector of quoted character strings
text.quoted <- dQuote(text)
message(text.quoted)
## "check"