Raja - 2 years ago 131
R Question

Explanation needed for v <- 2*x + y + 1

I am learning the R programming language with https://cran.r-project.org/doc/manuals/r-release/R-intro.pdf. The code below (p. 7, bottom of page) says 2*x will do 2.2 times, but what I can understand is 2*x says 2 multiplied with every element of X vector.

But the manual says 2.2 times; where is the 0.2 times coming from here? Or maybe I am looking at it in wrong way.

``````x <- c(10.4, 5.6, 3.1, 6.4, 21.7)
y <- c(x, 0, x)
v <- 2*x + y + 1
``````

Generates a new vector v of length 11 constructed by adding together, element by element, 2*x repeated 2.2 times, y repeated just once, and 1 repeated 11 times.

This might be a little more clear if you set `y <- rep(0,11)` so that y is still 11 long, but is now 0, so when you do the adding you can see exactly what happens with `2*x` (since adding the vector of 0's will do the replication, but not change the values).
The phrasing in what you quote is a little awkward in not making a clear distinction between multiplication (`2*`) and replication (2.2 times).