Generating Regular Sequences in R

R language has a number of facilities for generating commonly used sequences of numbers. There are a number of functions for generating regular sequences in R to perform data analysis tasks:

  • Colon Operator (:)
  • seq() Function
  • rep() Function

Generating Regular Sequences in R Language

Usually, the functions related to generating regular sequences in R are used to create index vectors, vectors of evenly spaced numbers, repeating the patterns, and creating sequences for plotting.

Colon Operator (:)

The colon operator generates a sequence of integers, for example, 1:30 is the vector c(1, 2, …, 29, 30). The colon operator has a high priority within an expression, for example, 2*1:15 is the vector c(2, 4, …, 28, 30).

Let set $n=10$ and then compare the sequences $1:n-1$ and $1:(n-1)$:

n = 10
1:n-1
1:(n-1)
Generating Regular Sequences in R Language

The 30:1 may be used to generate a sequence backward.

30:1
## Output
 [1] 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10  9  8  7  6
[26]  5  4  3  2  1

The seq() Function

The seq() functions offer more flexibility and control over generating sequences. The seq() functions have five arguments, some of which may be specified in any call. The first two arguments of the function specify the beginning and end of the sequence.

Like other R functions, the arguments to seq() can also given in named form, in which case the order in which they appear is irrelevant. The first two arguments of seq() functions may be named from=value and to=value. Therefore seq(1, 30), seq(from = 1, to = 30) and seq(to = 30, from = 1) are all the same as 1:30. The other two arguments may be named by = value and length = value, which specify a step size and a length for the sequence, respectively. By default the by argument is set to 1, that is, by = 1. The examples of seq() functions are

seq(1, 20)
## Output
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

seq(from = 1, to = 20)
## Output 
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

seq(from = 1, to = 20, by = 1)
## OUtput
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

seq(-5, 5, by = 0.2)
##
 [1] -5.0 -4.8 -4.6 -4.4 -4.2 -4.0 -3.8 -3.6 -3.4 -3.2 -3.0 -2.8 -2.6 -2.4 -2.2
[16] -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2  0.0  0.2  0.4  0.6  0.8
[31]  1.0  1.2  1.4  1.6  1.8  2.0  2.2  2.4  2.6  2.8  3.0  3.2  3.4  3.6  3.8
[46]  4.0  4.2  4.4  4.6  4.8  5.0

seq(length = 51, from = -5, by = 0.2)

Note that if only the first two arguments are given the result is the same as the colon operator. For example, seq(2, 10) results in the same output as 2:10.

The length.out argument may be used to generate a sequence of evenly spaced numbers, for example,

# generate a sequence of evenly spaced numbers between 0 and 1
seq(from = 0, to = 1, length.out = 11) 

## Output
[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

The fifth argument may be named along = vector, which is normally used as the only argument to create the sequence 1,2, …., length(vector) or the empty sequence if the vector is empty. For example

x = rnorm(10)
seq(along = x)

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

The rep() Function

The rep function is used for replicating or repeating an object in various complicated ways. The simplest form of the rep() function is

rep(1:5, times = 5)

## Output
[1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

The rep(1:5, times = 5) will put five copies of 1:5 end-to-end. The other useful version of rep() function is

rep(1:5, each = 5)

## Output
[1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5

The rep(1:5, each = 5) repeats each element of 1:5 five times before moving on to the next number.

Frequently Asked Questions About R, generating regular sequences in R

R Language Quiz

General Knowledge Quizzes

Statistics and Data Analysis

Frequently Asked Questions about Generating Sequences

  • Describe R functions that are used to generate regular sequences.
  • What is the use of seq() function in R?
  • Give some examples of colon operators in R?
  • Describe rep() function in R with examples.
  • What is the length.out argument in seq() function?
  • Write about important arguments of seq() function in R language.
  • How one can generate a sequence backward, give an example.

Frequency Table in R: Factors Variable

Recall that in R language a factor is a variable that defines a partition into groups. A single factor variable can be used to create a simple frequency table in R, while a pair of factors can be used to define a two-way cross-classification (contingency or frequency distribution). For this purpose, the table() function allows to creation of frequency tables. The frequency table is calculated from equal length factors.

Frequency Table in R of Categorical/ Group/ Factor Variable

We will use the “mtcars” dataset. For the variable $gear$, let us create a frequency table using the table() function. The table() function will count the gear code for each entry in the data vector. For example,

attach(mtcars)

freq <- table(gear)
freq
frequency table using factor

The freq object will give a table of frequencies of each gear code in the sample. It is important to note that, the frequencies are ordered and labeled by the levels attribute of the factor.

Frequency Distribution of a Continuous Variable

One can also create a frequency distribution table for a continuous variable. Suppose from the mtcars data set, we are interested in creating a frequency table of $mpg$ variable. For this purpose, first, we need to define the cut points or bins to define the classes/groups of the frequency table. For example,

cut(mpg, 10+5*(0:5))

## Output
(10,15] (15,20] (20,25] (25,30] (30,35] 
      6      12       8       2       4 

The cut() function is used to split the continuous data vector into groups. The groups are defined by creating a sequence of values using 10+5*(0:5), that is

10+5*(0:5)

## Output
10 15 20 25 30 35

The cut() function, cuts and counts the occurrence of each observation of mpg regarding the cut points created using breaks = 10+5*(0:5). The frequency table will be

frequency table of a continuous variable

Creating Graph of Frequency Table

For the frequency table created above, one can easily create different graphical representations, such as pie charts and bar plots of the frequency table. For example,

freq<-table(cut(mpg, 10+5*(0:5)))
pie(freq)
hist(freq)
barplot(freq)
plot(freq)
Bar plot frequency table in R
bar plot in R language
pie chart in frequency table in R language

Note that: for a $k$ factor argument, the result is a $k$-way array of frequencies.

https://itfeature.com, https://gmstat.com

Types of Objects in R

R language operates on entities which are known as objects. There are various types of objects in R exists, such as vectors, matrices, factors, lists, data frames, functions, etc. In R, objects are classified into several types based on their structure and content.

Types of Objects in R

Matrices

Arrays or matrices are multi-dimensional generalizations of vectors. Matrices are vectors indexed by two or more indices and displayed specially. Matrices contain rows and columns of homogeneous elements. The class of matrices object is “matrix”. See more about matrices by following the matrices.

Factors

Factors are used to handle categorical data. Factor variables may contain two or more levels, used to define the group or category of the variable. See more about factors in detail by following factors.

Lists

Lists are a general form of vectors in which the various elements need not be of the same type, that is, lists may contain heterogeneous data. Lists are often vectors or lists themselves. Lists are a convenient way to get different results from statistical computation, as lists may contain different types of data objects. See more about lists by following the link Lists.

Data Frame

Data frame objects are similar to matrix object structures. Unlike matrix objects, the data frame objects may contain different types of objects, that is, heterogeneous data. Think of the data frame as “Data Matrices” with one row per observational unit but with (possibly) both numerical and categorical variables. Many experiments are best described by data frames, the treatments are categorical but the response (output) is numeric. For more details about the data frame, follow the link data frame.

Functions

Functions are themselves objects. In R Language, functions can be stored in the project’s workspace. Functions provide a quick, simple, and convenient way to extend the functionality and power of R. See more about functions and customization of functions, see Functions.

Examples of Different Types of Objects in R

# Scalar types
x <- 5        # Numeric (integer)
y <- 3.14159  # Numeric (double)
z <- "Hello"  # Character
b <- TRUE     # Logical

# Vector types
numbers <- c(1, 2, 3, 4)                  # Numeric vector
fruits <- c("apple", "banana", "orange")  # Character vector
bools <- c(TRUE, FALSE, TRUE)             # Logical vector

# Data frame
df <- data.frame(
  name = c("Ali", "Babar", "Usman"),
  age = c(25, 30, 28),
  city = c("Multan", "Lahore", "Karachi")
)

# Matrix
mat <- matrix(1:9, nrow = 3, ncol = 3)

# List
my_list <- list(
  numbers = numbers,
  fruits = fruits,
  df = df
)

# Factor
colors <- factor(c("red", "blue", "green", "red"))
Types of Objects in R Language

https://itfeature.com, https://gmstat.com