I meant something like:

TP <- 1:4
lapply(TP, function(x) {
    print(x)
    print(x^2)
})

You may wish to add cat("\n") after print(x^2) so that your results
from each iteration are separated.
You may also wish to add invisible() around lapply() if you're not
saving / / using the return list in any way.

On Mon, Nov 7, 2022 at 12:38 PM akshay kulkarni <akshay...@hotmail.com> wrote:
>
> Dear Andrew
>                          It doesn't work:
>
> > lapply(TP,function(x){print(x^2)})
> [1] 1
> [1] 4
> [1] 9
> [1] 16
> [[1]]
> [1] 1
>
> [[2]]
> [1] 4
>
> [[3]]
> [1] 9
>
> [[4]]
> [1] 16
>
> Basically, lapply() is implemented by a for loop. So there must be some way 
> right?
>
> tHanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
> ________________________________
> From: Andrew Simmons <akwsi...@gmail.com>
> Sent: Monday, November 7, 2022 10:50 PM
> To: akshay kulkarni <akshay...@hotmail.com>
> Cc: R help Mailing list <r-help@r-project.org>
> Subject: Re: [R] print and lapply....
>
> put print() around x^2
>
> On Mon, Nov 7, 2022, 12:18 akshay kulkarni <akshay...@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@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.

______________________________________________
R-help@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.

Reply via email to