将某些部分放在 R 中的双引号中
Putting Some Part in Double Quotes in R
我想将对象的某些部分放入双引号中,如下例所示:
需要输出
"Group 1" = 3, "Group 2" = 3
MWE
Test <- structure("Group 1 = 3, Group 2 = 3", class = "noquote")
Test
[1] Group 1 = 3, Group 2 = 3
as.character(Test)
[1] "Group 1 = 3, Group 2 = 3"
已编辑
其实我有一个很长的字符串(这里是Labs)
Labs <- c("Group 1", "Group 2")
Test <- noquote(paste(Labs, "= 3", collapse = ", "))
Test
[1] Group 1 = 3, Group 2 = 3
但是,我想要这样的输出
"Group 1" = 3, "Group 2" = 3
您可以使用单引号让 R 知道字符串的开始和结束位置。这会让你在里面有双引号:
Test <- c('"Group 1" = 3', '"Group 2" = 3')
如果您 print
它,那么默认情况下它会向您显示转义字符。但是,您可以 cat
它,或使用一些更高级的选项,具体取决于您的需要。
cat(Test)
"Group 1" = 3 "Group 2" = 3
我想将对象的某些部分放入双引号中,如下例所示:
需要输出
"Group 1" = 3, "Group 2" = 3
MWE
Test <- structure("Group 1 = 3, Group 2 = 3", class = "noquote")
Test
[1] Group 1 = 3, Group 2 = 3
as.character(Test)
[1] "Group 1 = 3, Group 2 = 3"
已编辑
其实我有一个很长的字符串(这里是Labs)
Labs <- c("Group 1", "Group 2")
Test <- noquote(paste(Labs, "= 3", collapse = ", "))
Test
[1] Group 1 = 3, Group 2 = 3
但是,我想要这样的输出
"Group 1" = 3, "Group 2" = 3
您可以使用单引号让 R 知道字符串的开始和结束位置。这会让你在里面有双引号:
Test <- c('"Group 1" = 3', '"Group 2" = 3')
如果您 print
它,那么默认情况下它会向您显示转义字符。但是,您可以 cat
它,或使用一些更高级的选项,具体取决于您的需要。
cat(Test)
"Group 1" = 3 "Group 2" = 3