Raja - 1 year ago 110

R Question

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.

Please help understanding this expression.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

The 2.2 is the number of times that x will be repeated (not what x will be multiplied by). In your example x has length 5 and y has length 11. The 2.2 comes because 2.2 times 5 is 11, so in order to have 2 vectors of the same length to add together, the shorter one (the result of 2*x) which has length 5 has to be repeated (the 2.) then one more element taken from that vector to make the total length 11.

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).

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**