R recycles the shorter one to match the longer one: 1 2 3 4 5 6 7 8 9 10 + 1 2 1 2 1 2 1 2 1 2 2 4 4 6 6 8 8 10 10 12 R does this recycling in many cases, and it can sometimes trap the unwary. Sarah On Thu, May 28, 2009 at 4:00 PM, bogaso.christofer <bogaso.christofer at gmail.com> wrote:> I have following addition : > > > >> 1:2 + 1:10 > > ?[1] ?2 ?4 ?4 ?6 ?6 ?8 ?8 10 10 12 > > > > I could not understand how R adding those two unequal vector? Any help? >-- Sarah Goslee http://www.functionaldiversity.org
I have following addition :> 1:2 + 1:10[1] 2 4 4 6 6 8 8 10 10 12 I could not understand how R adding those two unequal vector? Any help? [[alternative HTML version deleted]]
On 29/05/2009, at 8:00 AM, bogaso.christofer wrote:> I have following addition : > > > >> 1:2 + 1:10 > > [1] 2 4 4 6 6 8 8 10 10 12 > > > > I could not understand how R adding those two unequal vector? Any > help?Look at the help for ``+'' (?"+") and look at ``Value''. There you will see:> These operators return vectors containing the result of the > element by element operations. The elements of shorter vectors > are recycled as necessary (with a 'warning' when they are recycled > only _fractionally_). The operators are '+' for addition, '-' for > subtraction, '*' for multiplication, '/' for division and '^' for > exponentiation.The key word is ``recycled''. cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
recycling rule: repeat the shorter element as many times as necessary, all.equal(1:2 + 1:10 , rep(1:2, length=10) + 1:10) # TRUE HTH, baptiste On 28 May 2009, at 22:00, bogaso.christofer wrote:> I have following addition : > > > >> 1:2 + 1:10 > > [1] 2 4 4 6 6 8 8 10 10 12 > > > > I could not understand how R adding those two unequal vector? Any > help? > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code._____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag
bogaso.christofer wrote:> I have following addition : > > > > >> 1:2 + 1:10 >> > > [1] 2 4 4 6 6 8 8 10 10 12 > > > > I could not understand how R adding those two unequal vector? Any help? > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >Hi, R recycles the shorter vector to yield: 1+1 = 2 2+2 = 4 3+1 = 4 4+2 = 6 5+1 = 6 6+2 = 8 and so on. Is this what you wanted to know? Cheers, -- *Luc Villandr?* /Biostatistician McGill University Health Center - Montreal Children's Hospital Research Institute/