# [R] R question

Ross Ihaka ihaka at stat.auckland.ac.nz
Thu Dec 16 19:49:17 CET 1999

```On Thu, Dec 16, 1999 at 02:41:25PM -0500, Faheem Mitha wrote:
> I have the following question, which is elementary but I am unable to
> In a for(i=10) loop, I am trying to represent the 10 1-dimensional vectors
> l1, l2,... l10 by some expression that will run through these values.
> ie. soppose I want to add l1 + ... + l10
> I could go
>
> x <- 0
> for(i in 1:10){ x <- x+ l(i)}
>
> This should return x to be the sum of the 10 li's for i from 1 to 10
> except of course I'm doing something more complicated.
> But l(i) of course does not serve to represent l1 etc? What should I do?

Subsetting in S (and hence R) uses [] for subsetting.  What you need is

x <- 0
for(i in 1:10) x <- x + l[i]

(If the values are really in a vector, x <- sum(l) is rather faster).

Ross
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```