r - 使用 cat 输出向量元素序列
问题描述
我需要使用 R 中的 cat 函数获取某些输出语句。我编写了以下代码
it <-1
w <- c(1,2,3)
cat("\nUsing the eq(s)",w,"the iter is:",it,"\n",sep=",")
这给了我以下输出
Using the eq(s),1,2,3,the iter is:,1,
如果你能帮忙,我需要得到这个输出
Using the eq(s) 1, 2 and 3, the iter is: 1
谢谢
解决方案
稍微更通用(对于 的情况length(w) != 3
):
enlist <- function(x) {
n <- length(x)
if (n <= 1) return(x)
paste(toString(x[-n]), "and", x[n])
}
cat("Using the eq(s) ", enlist(w), ", the iter is: ", it, "\n", sep = "")
Using the eq(s) 1, 2 and 3, the iter is: 1
推荐阅读
- java - Hackerrank Mark 和 Toys 质疑我的解决方案不适用于大型输入测试用例
- c# - 远程调试 - .NET Core 调试器不支持 Windows PDB - 如何正确发布?
- php - 将附加的 css 动态保存在单独的文件中 - Wordpress
- c# - C# & NodeJS PBKDF 加密
- python - 你如何在谷歌colab上安装poppler
- python - 从其他 JSON 文件更新 JSON 格式
- ios - Wowza GoCoder 直播不能与其他 RTMP 一起使用
- ios - 如何在 Xcode 构建完成时设置警报?
- javascript - Angular 代码块中的代码 { [s: string]: boolean } 是什么?
- postgresql - 如何循环遍历 pg/PLSQL 中的列列表?