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
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 5No. 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, > simoneNo....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