我想使用循环函数输出这样的输出:
> stat-1, stat-2, stat-3, stat4, stat5.
目前这是我的代码:
x<-0;
while (x <= 10)
{
x <- x+1
z <- paste('stat-', x, collapse = "," )
print(z)
}
但我得到这样的输出:
[1] "stat- 1"
[1] "stat- 2"
[1] "stat- 3"
[1] "stat- 4"
[1] "stat- 5"
如何以单行显示输出?
答案 0 :(得分:5)
您不需要for
循环:
x <- 1:5
paste0("stat-", x, collapse = ", ")
# [1] "stat-1, stat-2, stat-3, stat-4, stat-5"
如果你想要一个终端“。”:
paste0(paste0("stat-", x, collapse = ", "), ".")
# [1] "stat-1, stat-2, stat-3, stat-4, stat-5."
答案 1 :(得分:0)
如果你想要几个字符串,你也可以尝试:
x<-0;
z<-NULL;
while (x <= 10) {
x <- x+1
z <- c(z,paste('stat-', x, collapse = "," ))
}
print(z)