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 > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide 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 nam10.safelinks.protection.outlook.com/?url=https://stat.ethz.ch/mailman/listinfo/r-help&data=05|01|rmh@temple.edu|9409e175bc7045c094c408da61caa961|716e81efb52244738e3110bd02ccf6e5|0|0|637929820309666159|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|3000|||&sdata=dKfATg8gOlTDpLqxUFgUSJ1l8+nr3qNakRxPZcFSRdE=&reserved=0 PLEASE do read the posting guide nam10.safelinks.protection.outlook.com/?url=http://www.r-project.org/posting-guide.html&data=05|01|rmh@temple.edu|9409e175bc7045c094c408da61caa961|716e81efb52244738e3110bd02ccf6e5|0|0|637929820309666159|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|3000|||&sdata=cFyypcyUFM3eRJWnJ/FzUkcc/tEjAYl4rJzOkPo29z0=&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 > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.