put print() around x^2 On Mon, Nov 7, 2022, 12:18 akshay kulkarni <akshay_e4 at 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 at 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]]
Another option is use paste() within print() lapply(TP,function(x){print(paste("x= ",x, " x^2 = ", x^2))}) Tim -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Andrew Simmons Sent: Monday, November 7, 2022 12:21 PM To: akshay kulkarni <akshay_e4 at hotmail.com> Cc: R help Mailing list <r-help at r-project.org> Subject: Re: [R] print and lapply.... [External Email] put print() around x^2 On Mon, Nov 7, 2022, 12:18 akshay kulkarni <akshay_e4 at 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 at 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%7C28ae59febb2d43ce03b108dac0e46e79%7C0d4da0f84a314d76ace60a62331e > 1b84%7C0%7C0%7C638034384601744085%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w > LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C > &sdata=1D8FQ07q2NcYL8PaJW84PqdUAte3pZJy8XJuJXENbJ4%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% > 7C28ae59febb2d43ce03b108dac0e46e79%7C0d4da0f84a314d76ace60a62331e1b84% > 7C0%7C0%7C638034384601744085%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwM > DAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C& > sdata=aEeZJ8FHVKGX%2BqOJaKskC1onjqKcON2Ux5cj3MimTGw%3D&reserved=0 > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]] ______________________________________________ R-help at 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%7C28ae59febb2d43ce03b108dac0e46e79%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638034384601744085%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=1D8FQ07q2NcYL8PaJW84PqdUAte3pZJy8XJuJXENbJ4%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%7C28ae59febb2d43ce03b108dac0e46e79%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638034384601744085%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=aEeZJ8FHVKGX%2BqOJaKskC1onjqKcON2Ux5cj3MimTGw%3D&reserved=0 and provide commented, minimal, self-contained, reproducible code.
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 <akwsimmo at gmail.com> Sent: Monday, November 7, 2022 10:50 PM To: akshay kulkarni <akshay_e4 at hotmail.com> Cc: R help Mailing list <r-help at r-project.org> Subject: Re: [R] print and lapply.... put print() around x^2 On Mon, Nov 7, 2022, 12:18 akshay kulkarni <akshay_e4 at hotmail.com<mailto:akshay_e4 at 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 at r-project.org<mailto:R-help at 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]]
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 at gmail.com> wrote:> > put print() around x^2 > > On Mon, Nov 7, 2022, 12:18 akshay kulkarni <akshay_e4 at 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 at 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 at 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.