How R automatically recycles, or repeats, elements of the shorter Vector

When applying an operation to two vectors that requires them to be the same length, R automatically recycles, or repeats, elements of the shorter one, until it is long enough to match the longer Vector.

Example 1:

Suppose we have two Vectors c(1,2,4) , c(6,0,9,10,13), where the first one is shorter with only 3 elements. Now if we sum these two, we will get a warning message as follows.

> c(1,2,4) + c(6,0,9,10,13)

[1] 7 2 13 11 15

Warning message:

In c(1, 2, 4) + c(6, 0, 9, 10, 13) : longer object length is not a multiple of shorter object length

Here R , Sum those Vectors by Recycling or repeating the elements in shorter one, until it is long enough to match the longer one as follows..

> c(1,2,4,1,2) + c(6,0,9,10,13)

[1] 7 2 13 11 15

Example 2 :

Here we will sum the an Vector with a Matrix.First we create a 3 by 2(3 rows;2 columns) Matrix as follows..

> x <- matrix(c(1,2,3,4,5,6),nrow=3,ncol=2)

or you can declare the same as follows, with only row specification, and the columns will decide by R automatically.

> x <- matrix(c(1,2,3,4,5,6),nrow=3)

> x

[,1] [,2]

[1,] 1 4

[2,] 2 5

[3,] 3 6

The Matrices are actually long vectors. Here, x, is a 3-by-2 matrix. The Matrix actually equal to a 6 elements vector c(1,2,3,4,5,6).

Now we will add a 2 element Vector with this 6 element Matrix

> x+c(1,2)

[,1] [,2]

[1,] 2 6

[2,] 4 6

[3,] 4 8

Now the R has Recycled or Repeated the 2 element Shorter vector until it is long enough to match the longer one, 6 element Matrix as follows.

x + c(1,2,1,2,1,2)

Also note that the vector c(1,2,1,2,1,2) was changed from a vector to a matrix having the same shape as x before the addition took place.

Now let’s look at some common operations related to vectors. We’ll cover arithmetic and logical operations, vector indexing, and some useful ways to create vectors. Then we’ll look at two extended examples of using these operations.

Remember that R is a functional language.Every operator, including + in the following example, is actually a function.

>3+5

[1] 8

> "+"(3,5)

[1] 5

Please note that scalars are actually one-element vectors. So, we can add vectors, and the + operation will be applied element-wise.

> x <- c(1,2,5)

> x + c(5,0,-1)

[1] 6 2 4

Now we multiply the Vectors

> x * c(5,0,-1)

[1] 6 0 -5

Please note that the way the * function is applied, the multiplication is done element by element. The first element of the product (5) is the result of the first element of x (1) being multiplied by the first element of c(5,0,1) (5), and so on.

The same principle applies to other numeric operators. Here’s an example:

> x <- c(1,2,4)

> x / c(5,4,-1)

[1] 0.2 0.5 -4.0

> x %% c(5,4,-1)

[1] 1 2 0

When applying an operation to two vectors that requires them to be the same length, R automatically recycles, or repeats, elements of the shorter one, until it is long enough to match the longer Vector.

Example 1:

Suppose we have two Vectors c(1,2,4) , c(6,0,9,10,13), where the first one is shorter with only 3 elements. Now if we sum these two, we will get a warning message as follows.

> c(1,2,4) + c(6,0,9,10,13)

[1] 7 2 13 11 15

Warning message:

In c(1, 2, 4) + c(6, 0, 9, 10, 13) : longer object length is not a multiple of shorter object length

Here R , Sum those Vectors by Recycling or repeating the elements in shorter one, until it is long enough to match the longer one as follows..

> c(1,2,4,1,2) + c(6,0,9,10,13)

[1] 7 2 13 11 15

Example 2 :

Here we will sum the an Vector with a Matrix.First we create a 3 by 2(3 rows;2 columns) Matrix as follows..

> x <- matrix(c(1,2,3,4,5,6),nrow=3,ncol=2)

or you can declare the same as follows, with only row specification, and the columns will decide by R automatically.

> x <- matrix(c(1,2,3,4,5,6),nrow=3)

> x

[,1] [,2]

[1,] 1 4

[2,] 2 5

[3,] 3 6

The Matrices are actually long vectors. Here, x, is a 3-by-2 matrix. The Matrix actually equal to a 6 elements vector c(1,2,3,4,5,6).

Now we will add a 2 element Vector with this 6 element Matrix

> x+c(1,2)

[,1] [,2]

[1,] 2 6

[2,] 4 6

[3,] 4 8

Now the R has Recycled or Repeated the 2 element Shorter vector until it is long enough to match the longer one, 6 element Matrix as follows.

x + c(1,2,1,2,1,2)

Also note that the vector c(1,2,1,2,1,2) was changed from a vector to a matrix having the same shape as x before the addition took place.

Now let’s look at some common operations related to vectors. We’ll cover arithmetic and logical operations, vector indexing, and some useful ways to create vectors. Then we’ll look at two extended examples of using these operations.

**Vector Arithmetic and Logical Operations**Remember that R is a functional language.Every operator, including + in the following example, is actually a function.

>3+5

[1] 8

> "+"(3,5)

[1] 5

Please note that scalars are actually one-element vectors. So, we can add vectors, and the + operation will be applied element-wise.

> x <- c(1,2,5)

> x + c(5,0,-1)

[1] 6 2 4

Now we multiply the Vectors

> x * c(5,0,-1)

[1] 6 0 -5

Please note that the way the * function is applied, the multiplication is done element by element. The first element of the product (5) is the result of the first element of x (1) being multiplied by the first element of c(5,0,1) (5), and so on.

The same principle applies to other numeric operators. Here’s an example:

> x <- c(1,2,4)

> x / c(5,4,-1)

[1] 0.2 0.5 -4.0

> x %% c(5,4,-1)

[1] 1 2 0

--------------------------------------------------------------------------------------------------------

Thanks, TAMATAM ; Business Intelligence & Analytics Professional

Thanks, TAMATAM ; Business Intelligence & Analytics Professional

--------------------------------------------------------------------------------------------------------

## No comments:

## Post a Comment

Hi User, Thank You for Visiting My Blog. Please Post Your Feedback/Comments/Query.