Tiago R Magalhaes
2004-Dec-01 18:01 UTC
[R] Combined variable names (two concrete suggestions)
Very interesting topic. "What I want to know is *WHY* people are doing this?" Here goes my view - I've used many for loops, and each time I realize how stupid that is... AFTER I learned how to avoid it. But it's difficult to avoid them without knowing how to do it. (Monsieur de LaPalice wouldn't have puted it better...) For loops are conceptually very easy to understand. Lists are not easy to understand (why list[[1]] instead of list[1]? it's not completely intuitive) . The concept of lapply is difficult: it involves a list, then most likely writing a function and pasting those two concepts togther. When I first came accross assign, I thought that was the coolest thing, to be able to output something for each loop. I thought I was smart and R was smart. And yet, lapply makes it way better in almost all the cases. I can make two very concrete suggestions: a) In "An Introduction to R" after: "Warning: for() loops are used in R code much less often than in compiled languages. Code that takes a `whole object' view is likely to be both clearer and faster in R." Give some examples how to avoid it. Mention subset, subscript, sub, gsub (to substitute for characters), lower.tri for lower triangle in matrixes and other nice tricks that people could use. Does this sound situations where I used for loops in some (not so far away) time ago? YES! b) in the assign help file: after the example: for(i in 1:6) { #-- Create objects 'r1', 'r2', ... 'r6' -- nam <- paste("r",i, sep=".") assign(nam, 1:i) } r.3 [1] 1 2 3 write a note: "the following would do the same more efficiently" i=1:6 r=lapply(i, function(i) {1:i}) r[[3]] [1] 1 2 3 Finally, I have the feeling that this type of problems are very common and I don't know how to categorize them. In the same category I would involve: get("x") that solved a lot of problems for me, do.call("function") that solved a lot of problems as well. This category deals basically with object names, attributes, strings, and so on. For instance, I am sure there is a way to get the name of an object something that does: function(x)="x". And I haven't found it yet. Many people are coming to R that don't really have computer science background. In that sense, you amazing people that know so much about this and that I read everyday in awe , are a victim of your own success. You have produced an amazing software for FREE!, easy to get access to, with a mailing list, packages that are becoming standard in (at least...) biology. And yet, this sofware is necessarily very technical and with a lot of fine points and details. A very nice conundrum. I am afraid we will keep asking a lot of questions. And hopefully you won't get bored and stop answering them. Tobias Muhlhofer <t.muhlhofer@lse.ac.uk> wrote: I am trying to define a large number of variables through a loop construct. He wants to do for (i in 1:100) { assign(paste("v", i, sep=""), ....something or other...) } This is, of course, a FAQ. It's such a FAQ that I must have seen it once a day for the last several days. What I want to know is *WHY* people are doing this? What, precisely, does it buy you to have variables called v1...v100 rather than variables called v[[1]]...v[[100]]? Apart from persistent inconvenience, that is? What, really, is wrong with v <- lapply(1:100, function (i) ....something or other...) [[alternative HTML version deleted]]
Douglas Bates
2004-Dec-01 23:41 UTC
[R] Combined variable names (two concrete suggestions)
Tiago R Magalhaes wrote:> For loops are conceptually very easy to understand. Lists are not > easy to understand (why list[[1]] instead of list[1]? it's not > completely intuitive) .I try to explain it as comparable to the difference between a subset that consists of one element (the "[" function always returns a vector of the same mode as its argument) and the element itself. Whether that explanation makes sense depends on how comfortable the listener is with mathematical abstractions. As they say, "There are 10 types of people in this world: those who understand binary numbers and those who don't."