Dear members,
I have the following code:
testprint <- function() {
for(i in 1:5) {for(j in 1:5)
{cat(j)}
print(i)}
}
And the output is:
> testprint()
12345[1] 1
12345[1] 2
12345[1] 3
12345[1] 4
12345[1] 5
Any idea on how to remove the [1] from the output, and give spaces in the cat
output? The desired output is:
1 2 3 4 5 1
1 2 3 4 5 2
1 2 3 4 5 3
1 2 3 4 5 4
1 2 3 4 5 5
Many thanks in advance.....
THanking you,
Yours sincreely,
AKSHAY M KULKARNI
[[alternative HTML version deleted]]
Skip the for loops: cat(paste( seq(1:5), ? ?, 1:5) ) ? David Sent from my iPhone> On Jul 9, 2022, at 9:47 AM, akshay kulkarni <akshay_e4 at hotmail.com> wrote: > > ?Dear members, > I have the following code: > > testprint <- function() { > > for(i in 1:5) {for(j in 1:5) > {cat(j)} > print(i)} > } > > And the output is: > >> testprint() > 12345[1] 1 > 12345[1] 2 > 12345[1] 3 > 12345[1] 4 > 12345[1] 5 > > Any idea on how to remove the [1] from the output, and give spaces in the cat output? The desired output is: > > 1 2 3 4 5 1 > 1 2 3 4 5 2 > 1 2 3 4 5 3 > 1 2 3 4 5 4 > 1 2 3 4 5 5 > Many thanks in advance..... > > THanking you, > Yours sincreely, > AKSHAY M KULKARNI > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Richard M. Heiberger
2022-Jul-09 17:01 UTC
[R] [External] printing with bothe print and cat...
for(i in 1:5) {
for(j in 1:5) cat(j, " ")
cat(" ", i, "\n")}
print() puts out the [1], so don?t use print().
cat puts out only what you tell it. So if you want a space, you must include
the space.
From: R-help <r-help-bounces at r-project.org> on behalf of akshay
kulkarni <akshay_e4 at hotmail.com>
Date: Saturday, July 09, 2022 at 12:47
To: R help Mailing list <r-help at r-project.org>
Subject: [External] [R] printing with bothe print and cat...
Dear members,
I have the following code:
testprint <- function() {
for(i in 1:5) {for(j in 1:5)
{cat(j)}
print(i)}
}
And the output is:
> testprint()
12345[1] 1
12345[1] 2
12345[1] 3
12345[1] 4
12345[1] 5
Any idea on how to remove the [1] from the output, and give spaces in the cat
output? The desired output is:
1 2 3 4 5 1
1 2 3 4 5 2
1 2 3 4 5 3
1 2 3 4 5 4
1 2 3 4 5 5
Many thanks in advance.....
THanking you,
Yours sincreely,
AKSHAY M KULKARNI
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Crmh%40temple.edu%7C9409e175bc7045c094c408da61caa961%7C716e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637929820309666159%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=dKfATg8gOlTDpLqxUFgUSJ1l8%2Bnr3qNakRxPZcFSRdE%3D&reserved=0
PLEASE do read the posting guide
https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Crmh%40temple.edu%7C9409e175bc7045c094c408da61caa961%7C716e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637929820309666159%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=cFyypcyUFM3eRJWnJ%2FFzUkcc%2FtEjAYl4rJzOkPo29z0%3D&reserved=0
and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
Hello,
Like this?
testprint <- function() {
for(i in 1:5) {
for(j in 1:5) {
cat(j, "")
}
cat("\t", i, "\n")
}
}
testprint()
#> 1 2 3 4 5 1
#> 1 2 3 4 5 2
#> 1 2 3 4 5 3
#> 1 2 3 4 5 4
#> 1 2 3 4 5 5
Hope this helps,
Rui Barradas
?s 17:46 de 09/07/2022, akshay kulkarni escreveu:> Dear members,
> I have the following code:
>
> testprint <- function() {
>
> for(i in 1:5) {for(j in 1:5)
> {cat(j)}
> print(i)}
> }
>
> And the output is:
>
>> testprint()
> 12345[1] 1
> 12345[1] 2
> 12345[1] 3
> 12345[1] 4
> 12345[1] 5
>
> Any idea on how to remove the [1] from the output, and give spaces in the
cat output? The desired output is:
>
> 1 2 3 4 5 1
> 1 2 3 4 5 2
> 1 2 3 4 5 3
> 1 2 3 4 5 4
> 1 2 3 4 5 5
> Many thanks in advance.....
>
> THanking you,
> Yours sincreely,
> AKSHAY M KULKARNI
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.