4

A Real newbie to R environment, I am going through the book "Introduction to R".

There, in an example, author suggests that "

Shorter vectors in the expression are recycled as often as need be (perhaps fractionally) until they match the length of the longest vector.

Immediately after that, there is this example... wherein it suggests that a vector would be repeated 2.2 times...

However, when I replicated the same example on my system (ubuntu 64b, R - v2.4.11), I got this error message

x
[1]  2  5  8  6 11
> y
[1] 23 11
> v=2*x+y+1
Warning message:
In 2 * x + y :
  longer object length is not a multiple of shorter object length
> v
[1] 28 22 40 24 46

Tried searching through google, stackoverflow internally as well, but couldnt find anything satisfactory... Am I missing something here ? is there something with the version of R I am using ?

Raghav
  • 2,128
  • 5
  • 27
  • 46
  • A warning is not an error. And in this case the shorter vector is replicated 2.5 times (not 2.2), to give a length 5 vector. – mnel Dec 20 '12 at 04:14
  • ok, point taken about warnings/error. in my situation then 2*x+y+1 should have how many elements ? I am getting only 5 in v (as i displayed the result). The book suggests a diff number... – Raghav Dec 20 '12 at 05:00
  • 1
    Surely the the book does not state it should be 11? The shortest vector is 2, it is fractionally replicated (within the call to `+` ) to give a vector of length 5 (the length of the longest vector is 5). The result should be length 5. – mnel Dec 20 '12 at 05:03
  • i am too new with R to know about the patching level etc.. If you think 2.4 is old, where do I get the latest version ? =========================================== Output of R --version raghav@deskubuntu:~$ R --version R version 2.14.1 (2011-12-22) Copyright (C) 2011 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under the terms of the GNU General Public License version 2. For more information about these matters see http://www.gnu.org/licenses/. – Raghav Dec 20 '12 at 05:06
  • 1
    It has nothing to do with the version of R you're running. http://cran.r-project.org is the homepage if you want the latest version. Don't expect anything to be different. What does your book suggest the output should be? – Señor O Dec 20 '12 at 05:19
  • got my mistake... :( was following "An Introduction to R", and mistook the vector y... read all of it again, and followed right this time around.. Appreciate the help got here.. Thanks a lot guys... – Raghav Dec 24 '12 at 10:52
  • My learning is that, the length of the result vector will most probably match the length of the largest constituent vector. In my example, y was supposed to be 11 elements, thats what I was confusing myself with.. I had changed the example with my own copy of y, with only two elements.. – Raghav Dec 24 '12 at 10:54

1 Answers1

9

When a vector is recycled, it will display a warning message if it has to be "cut off" before it's finished. (as mentioned below, an this is NOT an error message. Error = R can't complete the function you want it to and so it quits. Warning = R found something strange about what you're asking it to do but can still do it.*)

For example:

c(1,2) * c(1,2,3,4)

Is equivalent to:

c(1,2,1,2) * c(1,2,3,4)

And displays no warning message. But:

c(1,2) * c(1,2,3,4,5)

Is equivalent to:

c(1,2,1,2,1) * c(1,2,3,4,5)

And displays a warning message, since the last element of the coerced vector is not the last element in the original vector. It will still do the work and give you an answer. The warning is just a warning. See ?warning.

* See section 2 of this paper

Señor O
  • 17,049
  • 2
  • 45
  • 47
  • + too quick. It would be a good idea to make it clear that an warning is not an error. – mnel Dec 20 '12 at 04:15