R Data Types

R has a wide variety of data types. Here, we will mention some of the most frequently used data types. These are the most frequently used data types -

• Vectors
• Lists
• Matrices
• Arrays
• Factors
• Data Frames

Vectors

A vector represents a set of elements of the same mode, which can be integer, floating number, character, complex and so on. A vector is a basic data object in R. In R, a single number is also considered as one element vector instead of calling scalar vector. The concatenation function c, is used to create a vector. It binds the element together of the same type.

Example

The example below contains some numeric elements in a vector.

``````num <- c(10, 3, 4, 5, 6)
print(num)
[1] 10  3  4  5  6``````

The example below contains all string type elements in a vector.

``````animales <- c("dog", "cat", "elephant", "fox")
print(animales)
[1] "dog"      "cat"      "elephant" "fox"   ``````

We can also write logical vector, which values are either True and False. In R, TRUE and FALSE can also be abbreviated to T and F.

``logical <- c(T, T, F, T)``

Lists

R provides list() function to create lists which may contain different objects. We can also say that it can combine objects of different types. A list is a generic vector. A vector have all elements of the same type, but a list may contain elements of different types.

Example

The example below contain students data in a list and access the data by using print function.

``````l1 <- list(c(5, 3, 5), c('sophiya', 'somya', 'Jorz'), 'IT', age=12)
print(l1)
[[1]]
[1] 5 3 5

[[2]]
[1] "sophiya" "somya"   "Jorz"

[[3]]
[1] "IT"

\$age
[1] 12
``````

Matrices

A matrix (plural: matrices) is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns. It has two dimensional structure. All columns in a matrix are the same type and of the same length. In R, a matrix is created by using matrix() function. The number of rows and columns of the matrix is assigned in nrow and ncol.

Example

The example below creates a matrix of two rows and two columns.

``````y <- matrix(c(1,2,3,4),nrow=2,ncol=2)
print(y)
[,1] [,2]
[1,]    1    3
[2,]    2    4``````

Arrays

In R, array() function is used to create data objects which can store more than two dimensions.

Example

``````x <- array(1:12, dim=c(2,2,3))
print(x)
, , 1

[,1] [,2]
[1,]    1    3
[2,]    2    4

, , 2

[,1] [,2]
[1,]    5    7
[2,]    6    8

, , 3

[,1] [,2]
[1,]    9   11
[2,]   10   12
``````

Factors

Factors are a special variable type for storing categorical variables. A factor can contain both integers and strings and it is generally used in statistical modeling. The factor() function is used to create a factor.

Example

The example below contains a factor of working days.

``````f <- c("Mon", "Tues", "Wed", "Thu", "Fri")
fdata <- factor(f)
print(fdata)
[1] Mon  Tues Wed  Thu  Fri
Levels: Fri Mon Thu Tues Wed``````

Data Frames

A Data Frame is a list of vectors of equal length. It has two dimensional array like structure, in which the elements should be same in each column.

Example

The example below contains a data frame of students.

``````data <- data.frame(id = c(1:4), name = c("Mary", "Soy", "Alexa", "Roxy"), class = 5)
print(data)
id  name class
1  1  Mary     5
2  2   Soy     5
3  3 Alexa     5
4  4  Roxy     5
``````