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

Remove comma in last vector element using cat in R

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

我想使用 cat 在 cat 中使用 sep 参数在 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

只需在此处使用 pastecollapse

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

words not included: 
 foo,foo2,foo3

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

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

words not included: 
 foo, foo2, foo3