[R] print and lapply....

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Mon Nov 7 19:29:37 CET 2022


Às 17:17 de 07/11/2022, akshay kulkarni escreveu:
> 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.
Hello,

Here are two options, with ?cat and with ?message.


TP <- 1:4
lapply(TP, function(x){
   cat("x =", x, "x^2 =", x^2, "\n")
})

lapply(TP, function(x){
   msg <- paste("x =", x, "x^2 =", x^2)
   message(msg)
})


Hope this helps,

Rui Barradas



More information about the R-help mailing list