R involves various data structures. Some of these structures in R are scalar, vector, matrix, array, data frame and list. We go through these data structures in detail.
For correct computational process, a programming language must know the structure of data. Therefore, R needs to know data structure at hand. Some of R’s basic data structures require that all members of data types must be same (e.g. vectors, matrices). On the other hand, some of other data structures in R can contain multiple data types (e.g. lists, data frames). R’s basic data structures are given in a list below.
- Scalar
- Vector
- Matrix
- Array
- Data Frame
- List
In this article, we go through these data structure one-by-one. For practice, you can install R by following our steps for R installation.
Check Out: How to Download and Install R for Windows
1) Scalar in R
Firstly, we start with scalar in R. There exist different scalar types (data types) in R. These are numeric, integer, logical, complex and character data types in R.
a <- 3 # numeric
b <- "three" # character
c <- 3L # integer
d <- TRUE # logical
e <- 1+2i # complex
Also Check: What are Data Types in R?
2) Vector in R
All elements of a vector must have the same data type (numeric, factor, character, etc.). Therefore, c() function is used to create a vector in R.
a <- c(1,3,5.4,-4) # numeric vector
b <- c("one","two","three", "four") # character vector
c <- c(3L,-2L,4L,7L) # integer vector
d <- c(TRUE,FALSE,FALSE,TRUE) # logical vector
e <- c(-1+3i,2+2i,1-4i, 2+3i) # complex vector
You can touch any element in vector. For example, I changed the third element of vector d with TRUE.
d[3] <- TRUE
d
## [1] TRUE FALSE TRUE TRUE
The as.vector() function attempts to coerce its argument to be of vector type. The is.vector() function returns a logical, TRUE or FALSE, to understand whether the data structure is vector or not.
3) Matrix in R
All columns in a matrix must have the same data type (numeric, factor, character, etc.) and the same length. The matrix() function is used to create a matrix in R.
a <- matrix(1:6, nrow=2, ncol=3) # matrix
a
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
b <- matrix(1:6, nrow=2, ncol=3, byrow = T) # matrix filled by rows
b
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
You can also give row names and column names of a matrix in R.
c <- matrix(1:6, nrow=2, ncol=3, dimnames = list(c("R1","R2"), c("C1","C2","C3"))) # matrix with row and column names
c
## C1 C2 C3
## R1 1 3 5
## R2 2 4 6
You can pull any row or column from a matrix.
a[1,] # the first row of matrix
## [1] 1 3 5
a[,3] # the third column of matrix
## [1] 5 6
a[,c(1,3)] # the first and third colums of matrix
## [,1] [,2]
## [1,] 1 5
## [2,] 2 6
The as.matrix() function attempts to coerce its argument to be of matrix type. The is.matrix() function returns a logical whether the data structure is matrix or not.
Also Check: How to Change Working Directory in R
4) Array in R
Arrays are very similar to matrices.
a <- array(1:6, dim = c(2,3)) # array
a
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
However, arrays do not stop when the number of observations is not compatible to the dimensions. As an example, I have seven obsevations, but 2×3 matrix has six cells. Therefore, the first six observations are placed in the matrix.
b <- array(1:7, dim = c(2,3)) # array
b
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
The as.array() function attempts to coerce its argument to be of array type. The is.array() function returns a logical whether the data structure is array or not.
5) Data Frame in R
A data frame is more general than a matrix. Each column can contain different type of data at the same time (numeric, factor, string, etc.) with same length. For example, I create a data frame. Its columns include numeric, character, integer, logical and complex vectors, respectively.
a <- c(1,3,5.4,-4) # numeric vector
b <- c("one","two","three","four") # character vector
c <- c(3L,-2L,4L,7L) # integer vector
d <- c(TRUE,FALSE,FALSE,TRUE) # logical vector
e <- c(-1+3i,2+2i,1-4i,2+3i) # complex vector
mydataframe <- data.frame(a,b,c,d,e)
mydataframe
## a b c d e
## 1 1.0 one 3 TRUE -1+3i
## 2 3.0 two -2 FALSE 2+2i
## 3 5.4 three 4 FALSE 1-4i
## 4 -4.0 four 7 TRUE 2+3i
The as.data.frame() function attempts to coerce its argument to be of data frame type. The is.data.frame() function returns a logical whether the data structure is data frame or not.
Also Check: How to Install and Load a Package in R
6) List in R
A list provides a storage which can contain a variety of objects. For example, I create a list containing six elements: numeric, character, integer, logical, complex vectors and data frame, respectively.
a <- c(1,3,5.4,-4) # numeric vector
b <- c("one","two","three","four") # character vector
c <- c(3L,-2L,4L,7L) # integer vector
d <- c(TRUE,FALSE,FALSE,TRUE) # logical vector
e <- c(-1+3i,2+2i,1-4i,2+3i) # complex vector
mydataframe <- data.frame(a,b,c,d,e)
mylist <- list(a,b,c,d,e,mydataframe)
mylist
## [[1]]
## [1] 1.0 3.0 5.4 -4.0
##
## [[2]]
## [1] "one" "two" "three" "four"
##
## [[3]]
## [1] 3 -2 4 7
##
## [[4]]
## [1] TRUE FALSE FALSE TRUE
##
## [[5]]
## [1] -1+3i 2+2i 1-4i 2+3i
##
## [[6]]
## a b c d e
## 1 1.0 one 3 TRUE -1+3i
## 2 3.0 two -2 FALSE 2+2i
## 3 5.4 three 4 FALSE 1-4i
## 4 -4.0 four 7 TRUE 2+3i
The as.list() function attempts to coerce its argument to be of list type. The is.list() function returns a logical whether the data structure is list or not.
The application of the codes is available in our youtube channel below.
Don’t forget to check: How to Import Data into R
0 Comments
5 Pingbacks