使用R中的cat删除最后一个向量元素中的逗号

问题描述 投票:0回答:2

我在R中面临一个小问题,但我不知道如何解决它。

我想使用cat在sep中使用cat参数在R控制台中打印消息。这是我尝试过的:

words <- c("foo", "foo2", "foo3")
cat("words not included:", "\n", words, sep = ",")
#words not included:,
#,foo,foo2,foo3

我们可以看到逗号插入在:之后和foo之前。

然后我做了:

cat("words not included:", "\n", words, sep = ",", "\n")
#words not included:,
#,foo,foo2,foo3,

我想要的输出是:

#words not included:
    #foo,foo2,foo3
r cat
2个回答
0
投票

只需在此处将pastecollapse一起使用:

words <- c("foo", "foo2", "foo3")
paste("words not included:", "\n", paste(words, collapse=","))

[1] "words not included: \n foo,foo2,foo3"

0
投票

您可以使用toString()代替sep中的cat()参数。

cat("words not included:", "\n", toString(words),  "\n")

words not included: 
 foo, foo2, foo3 
© www.soinside.com 2019 - 2024. All rights reserved.