[R] print and lapply....

Ebert,Timothy Aaron tebert @end|ng |rom u||@edu
Mon Nov 7 21:45:42 CET 2022


Dear Akshay,
    I think we have provided several solutions to the question asked. Can you please adjust your question to more closely align with what you need. It would be nice if you can provide sufficient detail so that we can see how you have adapted our solutions and how these have not quite addressed this problem. 

Tim

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of akshay kulkarni
Sent: Monday, November 7, 2022 1:10 PM
To: Bert Gunter <bgunter.4567 using gmail.com>; Andrew Simmons <akwsimmo using gmail.com>
Cc: R help Mailing list <r-help using r-project.org>
Subject: Re: [R] print and lapply....

[External Email]

Dear Bert,
                   Actually, I am replacing a big for loop by the lapply() function, and report the progress:

lapply(TP, function(i) { BODY; print(i)})

Can you please adjust your solution in this light?

THanking you,
Yours sincerely,
AKSHAY M KULKARNI
________________________________
From: Bert Gunter <bgunter.4567 using gmail.com>
Sent: Monday, November 7, 2022 11:16 PM
To: Andrew Simmons <akwsimmo using gmail.com>
Cc: akshay kulkarni <akshay_e4 using hotmail.com>; R help Mailing list <r-help using r-project.org>
Subject: Re: [R] print and lapply....

Well... yes, of course. But assuming the sole purpose is to print the results and not to save them for further processing, the OP's approach seems rather painful. My preference would be to vectorize:

> print(cbind(TP, TPsq =TP^2), print.gap = 3)
       TP   TPsq
[1,]    1      1
[2,]    2      4
[3,]    3      9
[4,]    4     16

See ?print.default for details

-- Bert


On Mon, Nov 7, 2022 at 9:20 AM Andrew Simmons <akwsimmo using gmail.com> wrote:
>
> put print() around x^2
>
> On Mon, Nov 7, 2022, 12:18 akshay kulkarni <akshay_e4 using hotmail.com> wrote:
>
> > Dear members,
> >                              I have the following code and output:
> >
> > > TP <- 1:4
> > > lapply(TP,function(x){print(x);x^2})
> > [1] 1
> > [1] 2
> > [1] 3
> > [1] 4
> > [[1]]
> > [1] 1
> >
> > [[2]]
> > [1] 4
> >
> > [[3]]
> > [1] 9
> >
> > [[4]]
> > [1] 16
> >
> > How do I make the print function output x along with x^2, i.e not at 
> > the beginning but before each of x^2?
> >
> > Many thanks in advance....
> >
> > THanking you,
> > Yours sincerely
> > AKSHAY M KULKARNI
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fst
> > at.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%4
> > 0ufl.edu%7Cb726a89349ed4860265708dac0eb5d0a%7C0d4da0f84a314d76ace60a
> > 62331e1b84%7C0%7C0%7C638034414365456384%7CUnknown%7CTWFpbGZsb3d8eyJW
> > IjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C300
> > 0%7C%7C%7C&sdata=dQk1JPxWI4D0D6%2FSoC8LWw56749ulaFrGQc6affgBeM%3
> > D&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%7Ctebert%40ufl.
> > edu%7Cb726a89349ed4860265708dac0eb5d0a%7C0d4da0f84a314d76ace60a62331
> > e1b84%7C0%7C0%7C638034414365456384%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiM
> > C4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%
> > 7C%7C&sdata=KDskHBxmxe0DV9XO7GvkESemUNTq2w%2BT%2Fro%2Bgb4DS2w%3D
> > &reserved=0 and provide commented, minimal, self-contained, 
> > reproducible code.
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using 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%7Ctebert%40ufl
> .edu%7Cb726a89349ed4860265708dac0eb5d0a%7C0d4da0f84a314d76ace60a62331e
> 1b84%7C0%7C0%7C638034414365456384%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w
> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> &sdata=dQk1JPxWI4D0D6%2FSoC8LWw56749ulaFrGQc6affgBeM%3D&reserv
> ed=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%7Ctebert%40ufl.edu%
> 7Cb726a89349ed4860265708dac0eb5d0a%7C0d4da0f84a314d76ace60a62331e1b84%
> 7C0%7C0%7C638034414365612626%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwM
> DAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&
> sdata=OxZmpA4ORbfUq0gqM8ebZvK5bQTaiCwo0Y1ekn%2F3zBE%3D&reserved=0
> and provide commented, minimal, self-contained, reproducible code.

        [[alternative HTML version deleted]]

______________________________________________
R-help using 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%7Ctebert%40ufl.edu%7Cb726a89349ed4860265708dac0eb5d0a%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638034414365612626%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=OCyLwP3YBC3kfih9kaX6vEbPdzvAq1a48LeDRyqlYd0%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%7Ctebert%40ufl.edu%7Cb726a89349ed4860265708dac0eb5d0a%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638034414365612626%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=OxZmpA4ORbfUq0gqM8ebZvK5bQTaiCwo0Y1ekn%2F3zBE%3D&reserved=0
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list