tag:blogger.com,1999:blog-1275149608391671670.post5375707955074537724..comments2023-09-28T06:13:40.704-04:00Comments on SAS and R: Example 9.29: the perils of for loopsKen Kleinmanhttp://www.blogger.com/profile/09525118721291529157noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-1275149608391671670.post-26384171241411477392012-05-09T06:51:23.792-04:002012-05-09T06:51:23.792-04:00@Luis: completely agree. Ways that work are never ...@Luis: completely agree. Ways that work are never wrong. <br /><br />In this case, the for loop has several nice attributes: it is explicit about what it is doing. By comparison "outer" is really nice, but also obscure. <br /><br />The *apply family of functions of course agree, as they are built on for loops. I find that the only time i am forced to use these, however, is when I wish someone had taken the time to make a nicely polymorphic version of the function that I am wrapping apply around.<br /><br />More generally I find the apply family implementation difficult (mostly because these functions try to get so much done in a small space (datatype, margins, extra parameters, side effects).<br /><br />so<br />for i = 1 to n<br /> for j = 1 to i<br /> print "I must use a for loop today so that people know what I am doing :-)"<br /> }<br />}timhttps://www.blogger.com/profile/12707381996365946983noreply@blogger.comtag:blogger.com,1999:blog-1275149608391671670.post-66958715665200658152012-04-30T22:05:18.990-04:002012-04-30T22:05:18.990-04:00Just to clarify -- my solution does return a list,...Just to clarify -- my solution does return a list, but that's independent of the stars printed. cat() returns NULL invisibly and has the side effect of printing its arguments. lapply() aggregates these NULLs into a list. Richard 's solution gives something that can be manipulated later.Michael Weylandtnoreply@blogger.comtag:blogger.com,1999:blog-1275149608391671670.post-13304246149729539452012-04-30T20:28:41.209-04:002012-04-30T20:28:41.209-04:00I agree with you, Luis. Sometimes people get dogm...I agree with you, Luis. Sometimes people get dogmatic about the "right" way to do something, and lose sight of the fact that learning different ways to use the tools make for a more flexible craftsperson. <br /><br />If it were possible to teach R without showing the for loop syntax, the result would be students who are less flexible and some convoluted though possibly elegant code.Ken Kleinmanhttps://www.blogger.com/profile/09525118721291529157noreply@blogger.comtag:blogger.com,1999:blog-1275149608391671670.post-79345361102648689032012-04-30T18:34:45.493-04:002012-04-30T18:34:45.493-04:00While I agree that many times loops are not the be...While I agree that many times loops are not the best thing in R, I think for teaching purposes they are a lot easier to understand. For example, a fluent use of outer() relies on the student understanding some matrix algebra (outer products), which may not be the most common situation.<br /><br />Loops are demonized in matrix languages, but for small examples the performance penalty is minimal and the gains in understanding are big. I would tend to teach the loop first and then explain the workings of outer and show a big example where performance differences matter.Luishttp://quantumforest.comnoreply@blogger.comtag:blogger.com,1999:blog-1275149608391671670.post-22590097358327096652012-04-30T12:49:20.417-04:002012-04-30T12:49:20.417-04:00Just to clarify: the solution I gave does indeed r...Just to clarify: the solution I gave does indeed return a list, but that's not so much the point of the construct. This would have done the same thing without making a list (admittedly, less clear): <br /><br />apply(matrix(1:5), 1, function(x) cat(rep("*",x),"\n"))<br /><br />The somewhat useless list returned by my lapply() is actually rep(list(NULL), n) (i.e., a list of NULL n times over) -- the observed star-printing is a side effect of cat(), while the return value is actually an invisible NULL which only becomes visible when aggregated by lapply. <br /><br />If the stars need to be passed off to something else, Richard's solution actually returns a character matrix with the desired stars. <br /><br />Thanks for the post!<br /><br />MichaelMichael Weylandtnoreply@blogger.com