[R] printing with bothe print and cat...

Ebert,Timothy Aaron tebert @end|ng |rom u||@edu
Sat Jul 9 22:04:43 CEST 2022


Here is an alternative without for loops

n_rows <- 5
n_cols <- 5
p_df <- data.frame(matrix( rep(1:n_rows, n_cols), nrow=n_rows, byrow=TRUE), line_n=1:n_rows)
print(p_df)


If n_rows always equals n_cols then you could combine the first two lines

n_rows <- n_cols <- 5
p_df <- data.frame(matrix( rep(1:n_rows, n_cols), nrow=n_rows, byrow=TRUE), line_n=1:n_rows)
n_cols <- n_cols+1
print(p_df)

You might also notice that as.numeric(rownames(p_df)) returns the same values as line_n.


Tim
From: akshay kulkarni <akshay_e4 using hotmail.com>
Sent: Saturday, July 9, 2022 1:41 PM
To: Ebert,Timothy Aaron <tebert using ufl.edu>; David Winsemius <dwinsemius using comcast.net>
Cc: R help Mailing list <r-help using r-project.org>
Subject: Re: [R] printing with bothe print and cat...

[External Email]
Dear Tim,
                   Many thanks...

Yours sincerely
AKSHAYM KULKARNI
________________________________
From: Ebert,Timothy Aaron <tebert using ufl.edu<mailto:tebert using ufl.edu>>
Sent: Saturday, July 9, 2022 11:03 PM
To: akshay kulkarni <akshay_e4 using hotmail.com<mailto:akshay_e4 using hotmail.com>>; David Winsemius <dwinsemius using comcast.net<mailto:dwinsemius using comcast.net>>
Cc: R help Mailing list <r-help using r-project.org<mailto:r-help using r-project.org>>
Subject: RE: [R] printing with bothe print and cat...

Here is an alternative that makes a dataframe and then prints the dataframe.
n_rows <- 5
n_cols <- 5
p_mat<-matrix(0,nrow=n_rows,ncol=n_cols)
for(i in 1:n_rows) {
  for(j in 1:n_cols){
    p_mat[i,j]<-(j)
  }
}
p_df <- data.frame(p_mat,1:n_rows)
print(p_df)


Tim

-----Original Message-----
From: R-help <r-help-bounces using r-project.org<mailto:r-help-bounces using r-project.org>> On Behalf Of akshay kulkarni
Sent: Saturday, July 9, 2022 1:02 PM
To: David Winsemius <dwinsemius using comcast.net<mailto:dwinsemius using comcast.net>>
Cc: R help Mailing list <r-help using r-project.org<mailto:r-help using r-project.org>>
Subject: Re: [R] printing with bothe print and cat...

[External Email]

Dear David,
                    THe code given was a reprex, and unfortunately, I cannot skip the for loops: Its a very big  web scraping code. Any alternative?

Many thanks in advance...

Yours sincrely,
AKSHAY M KULKARNI

________________________________
From: David Winsemius <dwinsemius using comcast.net<mailto:dwinsemius using comcast.net>>
Sent: Saturday, July 9, 2022 10:28 PM
To: akshay kulkarni <akshay_e4 using hotmail.com<mailto:akshay_e4 using hotmail.com>>
Cc: R help Mailing list <r-help using r-project.org<mailto:r-help using r-project.org>>
Subject: Re: [R] printing with bothe print and cat...

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 using hotmail.com<mailto:akshay_e4 using 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 using r-project.org<mailto:R-help using r-project.org> mailing list -- To UNSUBSCRIBE and more, see
> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mail
> man_listinfo_r-2Dhelp&d=DwIGaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAs
> Rzsn7AkP-g&m=lceoyahibi7acrpoFW6uDxJ4j_QtB0NYjb7GNZu_e9WpZDZ4hdpYpgvtx
> sQZbqGe&s=m5EK_gr2QGFthC1Q_GqiGjSV05AZSmV-T1kQpq5KlUM&e=
> PLEASE do read the posting guide
> https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.or
> g_posting-2Dguide.html&d=DwIGaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeA
> sRzsn7AkP-g&m=lceoyahibi7acrpoFW6uDxJ4j_QtB0NYjb7GNZu_e9WpZDZ4hdpYpgvt
> xsQZbqGe&s=EdP6JP8fQVHC-xOJfowZ7VlJOlqWwnJBjAi8sZR2Els&e=
> and provide commented, minimal, self-contained, reproducible code.


        [[alternative HTML version deleted]]

______________________________________________
R-help using r-project.org<mailto:R-help using r-project.org> mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwIGaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=lceoyahibi7acrpoFW6uDxJ4j_QtB0NYjb7GNZu_e9WpZDZ4hdpYpgvtxsQZbqGe&s=m5EK_gr2QGFthC1Q_GqiGjSV05AZSmV-T1kQpq5KlUM&e=
PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwIGaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=lceoyahibi7acrpoFW6uDxJ4j_QtB0NYjb7GNZu_e9WpZDZ4hdpYpgvtxsQZbqGe&s=EdP6JP8fQVHC-xOJfowZ7VlJOlqWwnJBjAi8sZR2Els&e=
and provide commented, minimal, self-contained, reproducible code.

	[[alternative HTML version deleted]]



More information about the R-help mailing list