[R] print and lapply....

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Mon Nov 7 18:46:05 CET 2022


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://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.
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using 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.



More information about the R-help mailing list