Generating Vectors,Vector Sequences and Repeating Vector Constants in R Programming

There are a few R operators that are especially useful for creating vectors. The operator Colon( : ) is one that is generally used to generate the vectors.

> 5:8

[1] 5 6 7 8

> 5:1

[1] 5 4 3 2 1

Now, we looks at the operator precedence at the loop context

Example :

>for (i in 1:length(x))

{

# Your code here

}

> i <- 2

> 1:i-1 # this means (1:i) - 1, not 1:(i-1)

[1] 0 1

> 1:(i-1)

[1] 1

In the expression 1:i-1, the colon operator takes precedence over the subtraction. So, the expression 1:i is evaluated first, returning 1:2. R then subtracts 1 from that expression. That means subtracting a one-element vector from a two-element vector, which is done via recycling. The one-element vector (1) will be extended to (1,1) to be of compatible length with 1:2.Element-wise subtraction then yields the vector (0,1).

On the other hand,In the expression 1:(i-1), the parentheses have higher precedence than the colon. Thus, 1 is subtracted from i, resulting in 1:1, as

seen in the above example.

The seq() (or sequence) function, which generates a sequence in arithmetic progression.

> seq(10)

[1] 1 2 3 4 5 6 7 8 9 10

For instance, the 5:10 yields the vector(5,6,7,8,9,10), with the elements spaced one unit apart (6 − 5 = 1, 7 − 6= 1,and so on).

We can generate the above Sequence using seq() function as follows...

> seq(5,10)

[1] 5 6 7 8 9 10

By default the increment level is 1. You can also specify your own increment level as follows..

#Sequence with start from 5 , to 10 , increment by 2

> seq(5,10,2)

[1] 5 7 9

#Sequence with start from 5 , to 10 , increment by 3

> seq(5,10,3)

[1] 5 8

We can also specify the seq () function as follows...

> seq(from=5,to=15,by=3)

[1] 5 8 11 14

The spacing can be a non integer value, too, say 0.1.

> seq(from=1.1,to=2,length=10)

[1] 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0

Note:

We can generate the cumulative sum of sequence using cusum() function, as shown in the below example.

> seq(10)

[1] 1 2 3 4 5 6 7 8 9 10

> cumsum(seq(10))

[1] 1 3 6 10 15 21 28 36 45 55

> x<-seq(10)

> x

[1] 1 2 3 4 5 6 7 8 9 10

> cumsum(x)

[1] 1 3 6 10 15 21 28 36 45 55

One good use for seq() is to deal with the empty-vector problems that we face in dealing with a loop, as in below example.

for (i in 1:length(x))

If x is empty, this loop should not have any iterations, but it actually has two, since 1:length(x) evaluates to (1,0). We could fix this by writing the statement as follows:

for (i in seq(x))

You can see that seq(x) gives us the same result as 1:length(x) if x is not empty, but it correctly evaluates to NULL if x is empty, resulting in zero iterations in the above loop.

Example:

> x <- c(5,12,13)

> x

[1] 5 12 13

> seq(x)

[1] 1 2 3

> x <- NULL

> x

NULL

> seq(x)

integer(0)

The rep() (or repeat) function allows us to conveniently put the same constant into long vectors. The call form is rep(x,times), which creates a vector of times*length(x) elements, that is, times copies of x.

Example:

> x <- rep(8,4)

> x

[1] 8 8 8 8

> rep(c(5,12,13),3)

[1] 5 12 13 5 12 13 5 12 13

> rep(1:3,2)

[1] 1 2 3 1 2 3

There is also a named argument each, which interleaves the copies of x.

> rep(c(5,12,13),each=2)

[1] 5 5 12 12 13 13

The any() and all() functions are use to check whether any or all of their arguments or individual elements are satisfying a logical condition or not.

The any() function then reports whether any of those values is TRUE. The all() function works similarly and reports if all of the values are TRUE.

Example:

> x <- 1:10

> any(x>7)

[1] TRUE

> any(x>10)

[1] FALSE

> any(x>13)

[1] FALSE

> all(x>5)

[1] FALSE

> all(x>0)

[1] TRUE

> all(x>13)

[1] FALSE

R will process the logical operation as follows.

> any(x>7)

It first evaluates x > 7, and then yielding this:

(FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,TRUE,TRUE,TRUE)

There are a few R operators that are especially useful for creating vectors. The operator Colon( : ) is one that is generally used to generate the vectors.

> 5:8

[1] 5 6 7 8

> 5:1

[1] 5 4 3 2 1

Now, we looks at the operator precedence at the loop context

Example :

>for (i in 1:length(x))

{

# Your code here

}

> i <- 2

> 1:i-1 # this means (1:i) - 1, not 1:(i-1)

[1] 0 1

> 1:(i-1)

[1] 1

In the expression 1:i-1, the colon operator takes precedence over the subtraction. So, the expression 1:i is evaluated first, returning 1:2. R then subtracts 1 from that expression. That means subtracting a one-element vector from a two-element vector, which is done via recycling. The one-element vector (1) will be extended to (1,1) to be of compatible length with 1:2.Element-wise subtraction then yields the vector (0,1).

On the other hand,In the expression 1:(i-1), the parentheses have higher precedence than the colon. Thus, 1 is subtracted from i, resulting in 1:1, as

seen in the above example.

**1) Generating Vector Sequences with seq() Function:**The seq() (or sequence) function, which generates a sequence in arithmetic progression.

> seq(10)

[1] 1 2 3 4 5 6 7 8 9 10

For instance, the 5:10 yields the vector(5,6,7,8,9,10), with the elements spaced one unit apart (6 − 5 = 1, 7 − 6= 1,and so on).

We can generate the above Sequence using seq() function as follows...

> seq(5,10)

[1] 5 6 7 8 9 10

By default the increment level is 1. You can also specify your own increment level as follows..

#Sequence with start from 5 , to 10 , increment by 2

> seq(5,10,2)

[1] 5 7 9

#Sequence with start from 5 , to 10 , increment by 3

> seq(5,10,3)

[1] 5 8

We can also specify the seq () function as follows...

> seq(from=5,to=15,by=3)

[1] 5 8 11 14

The spacing can be a non integer value, too, say 0.1.

> seq(from=1.1,to=2,length=10)

[1] 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0

Note:

We can generate the cumulative sum of sequence using cusum() function, as shown in the below example.

> seq(10)

[1] 1 2 3 4 5 6 7 8 9 10

> cumsum(seq(10))

[1] 1 3 6 10 15 21 28 36 45 55

> x<-seq(10)

> x

[1] 1 2 3 4 5 6 7 8 9 10

> cumsum(x)

[1] 1 3 6 10 15 21 28 36 45 55

**Handling Null or Empty Vectors with seq() Function:**One good use for seq() is to deal with the empty-vector problems that we face in dealing with a loop, as in below example.

for (i in 1:length(x))

If x is empty, this loop should not have any iterations, but it actually has two, since 1:length(x) evaluates to (1,0). We could fix this by writing the statement as follows:

for (i in seq(x))

You can see that seq(x) gives us the same result as 1:length(x) if x is not empty, but it correctly evaluates to NULL if x is empty, resulting in zero iterations in the above loop.

Example:

> x <- c(5,12,13)

> x

[1] 5 12 13

> seq(x)

[1] 1 2 3

> x <- NULL

> x

NULL

> seq(x)

integer(0)

**2) Repeating Vector Constants with rep() :**The rep() (or repeat) function allows us to conveniently put the same constant into long vectors. The call form is rep(x,times), which creates a vector of times*length(x) elements, that is, times copies of x.

Example:

> x <- rep(8,4)

> x

[1] 8 8 8 8

> rep(c(5,12,13),3)

[1] 5 12 13 5 12 13 5 12 13

> rep(1:3,2)

[1] 1 2 3 1 2 3

There is also a named argument each, which interleaves the copies of x.

> rep(c(5,12,13),each=2)

[1] 5 5 12 12 13 13

**3) Using all() and any() in Vectors:**The any() and all() functions are use to check whether any or all of their arguments or individual elements are satisfying a logical condition or not.

The any() function then reports whether any of those values is TRUE. The all() function works similarly and reports if all of the values are TRUE.

Example:

> x <- 1:10

> any(x>7)

[1] TRUE

> any(x>10)

[1] FALSE

> any(x>13)

[1] FALSE

> all(x>5)

[1] FALSE

> all(x>0)

[1] TRUE

> all(x>13)

[1] FALSE

R will process the logical operation as follows.

> any(x>7)

It first evaluates x > 7, and then yielding this:

(FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,TRUE,TRUE,TRUE)

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

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 open Feedback only related to this Blog Posts. Please note that I cannot respond to the Anonymous Comments.