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
problem with for()
7 messages · Peter Dalgaard, Marc Schwartz, Spencer Graves +1 more
Simone Gabbriellini <ogabbrie at tin.it> writes:
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
No. Indexing starts at 1 in R. Next, figure out this:
sum=0
for(i in 5:-5){ sum[i] = i }
sum
[1] -5 -5 -5 -5 -4
O__ ---- Peter Dalgaard ??ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
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
Hint: help.search() -> "An Introduction to R" -> "Simple manipulations numbers and vectors" -> ... spencer graves
Peter Dalgaard wrote:
Simone Gabbriellini <ogabbrie at tin.it> writes:
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
No. Indexing starts at 1 in R. Next, figure out this:
sum=0
for(i in 5:-5){ sum[i] = i }
sum
[1] -5 -5 -5 -5 -4
Spencer Graves, PhD Senior Development Engineer PDF Solutions, Inc. 333 West San Carlos Street Suite 700 San Jose, CA 95110, USA spencer.graves at pdf.com www.pdf.com <http://www.pdf.com> Tel: 408-938-4420 Fax: 408-280-7915
how can I have a 0 evaluated in my loop then? it is important for my algorithm do you have any hints? simone Il giorno 03/ago/05, alle ore 23:37, Marc Schwartz ha scritto:
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
It would help to have an example of what it is you are trying to do. Importantly, keep separate the need to have zero be a value in a vector as opposed to using zero to index a vector. As I note below in my reply, you can have:
x <- 0:5
x
[1] 0 1 2 3 4 5
x ^ 2
[1] 0 1 4 9 16 25 Marc
On Thu, 2005-08-04 at 00:25 +0200, Simone Gabbriellini wrote:
how can I have a 0 evaluated in my loop then? it is important for my algorithm do you have any hints? simone Il giorno 03/ago/05, alle ore 23:37, Marc Schwartz ha scritto:
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
Marc, I did it with the simple trick of keepin separate vector values and index values, as you suggested in your mail it was simple, but you know, it's always simple when you've done it :) thank you very much, simone Il giorno 04/ago/05, alle ore 00:39, Marc Schwartz ha scritto:
It would help to have an example of what it is you are trying to do. Importantly, keep separate the need to have zero be a value in a vector as opposed to using zero to index a vector. As I note below in my reply, you can have:
x <- 0:5
x
[1] 0 1 2 3 4 5
x ^ 2
[1] 0 1 4 9 16 25 Marc On Thu, 2005-08-04 at 00:25 +0200, Simone Gabbriellini wrote:
how can I have a 0 evaluated in my loop then? it is important for my algorithm do you have any hints? simone Il giorno 03/ago/05, alle ore 23:37, Marc Schwartz ha scritto:
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