Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

longer object length is not a multiple of shorter object length [duplicate]

Tags:

r

ubuntu

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 ?

like image 456
Raghav Avatar asked Feb 18 '23 03:02

Raghav


1 Answers

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

like image 148
Señor O Avatar answered Feb 20 '23 16:02

Señor O