Re: [R] problem with for()

From: Marc Schwartz <MSchwartz_at_mn.rr.com>
Date: Thu 04 Aug 2005 - 07:37:30 EST

On Wed, 2005-08-03 at 23:24 +0200, Simone Gabbriellini wrote:
> Dear list,
> can someone tell me why this two pieces of code give me the same
> results?
>
> > for(i in 0:5){ sum[i] = i }
> > sum
> [1] 1 2 3 4 5
>
> > for(i in 1:5){ sum[i] = i }
> > sum
> [1] 1 2 3 4 5
>
> shouldn't the first one be
>
> 0 1 2 3 4 5
>
> thank you,
> simone

No....the indexing of R objects is 1 based. Thus your first loop tried to set i[0], which is a non-existent entry.

> i <- 0:5

> i

[1] 0 1 2 3 4 5

> i[0]

numeric(0)

> i[1]

[1] 0

HTH, Marc Schwartz



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Aug 04 07:43:13 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:39:40 EST