Reputation: 139
I want an output like this using a loop function:
> stat-1, stat-2, stat-3, stat4, stat5.
Currently this is my code:
x<-0;
while (x <= 10)
{
x <- x+1
z <- paste('stat-', x, collapse = "," )
print(z)
}
But iam getting output like this:
[1] "stat- 1"
[1] "stat- 2"
[1] "stat- 3"
[1] "stat- 4"
[1] "stat- 5"
How can i get the output in single line?
Upvotes: 1
Views: 11268
Reputation:
You don't need a for
loop:
x <- 1:5
paste0("stat-", x, collapse = ", ")
# [1] "stat-1, stat-2, stat-3, stat-4, stat-5"
If you want a terminal ".":
paste0(paste0("stat-", x, collapse = ", "), ".")
# [1] "stat-1, stat-2, stat-3, stat-4, stat-5."
Upvotes: 6
Reputation: 90
If you want several string you can also try :
x<-0;
z<-NULL;
while (x <= 10) {
x <- x+1
z <- c(z,paste('stat-', x, collapse = "," ))
}
print(z)
Upvotes: 2