我在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
只需在此处将paste
与collapse
一起使用:
words <- c("foo", "foo2", "foo3")
paste("words not included:", "\n", paste(words, collapse=","))
[1] "words not included: \n foo,foo2,foo3"
您可以使用toString()
代替sep
中的cat()
参数。
cat("words not included:", "\n", toString(words), "\n")
words not included:
foo, foo2, foo3