Data Structures in R
There are many types of objects / Data Structures to store R-object . The frequently used objects are –
- Data Frames
A vector is most basic data structure in R. It is collection same objects like character , logical, integer or numeric .
a <- c(1,2,3,4)
 1 2 3 4
We can check object “a” is vector or not .
We have created character vector “b” as :
A matrix is a collection of data elements arranged in two-dimensional rectangular layout.
We can check layout of matrix function by :
It opens description and syntax of matrix function in Help window.
Syntax of matrix is :
matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
data = Represent data
nrow = number of rows
ncol = number of columns
byrow= the matrix is filled by row
dimnames = to assign names to row and columns
Number of elements = nrow * ncol
We have nrow=3 .
We find ncol = 2 .
The default value of byrow is FALSE and dimnames is NULL .
mat <- matrix(c(1,0,4,2,-1),nrow= 3)
In this case, byrow = FALSE means we filled elements of matrix by column. So, elements are filled column -wise .
[,1] [,2] <- Number of column
[1,] 1 2 <-Number of row
[2,] 0 -1
[3,] 4 5
We represent matrix mat as mat[r,c] , where r is number of row and c is number of column.
mat[1,2] <- First row and second column element .
We can show all elements of whole row as m[r,] and whole column as m[,c] .
mat[1,] <- It shows all elements of first row
 1 2
mat[,2] <- It shows all elements of second column
 2 -1 5
We can also filled elements by row as :
It shows elements are filled in matrix by filling rows by rows.
We check the class of mat object by :
We can give names to columns and rows by using dimnames :
mat <- matrix(c(1,0,4,2,-1,5),nrow= 3,dimnames = list(c(“a”,”b”,”c”),c(“x”,”y”)))
Recycle means reusable of materials . We can reuse data to perform functions required .
We are creating a matrix having 5 elements. We assign number of rows are 2 . So , number of columns are 3.
You can see that , there is warning message which showing number of elements are not multiple of 2. So, it will recycle the remaining element by starting with first element to fill.
You can see here , we have create a matrix of 10 elements . The elements are repeated to fill remaining elements.
We can also create matrix on data object :
We can create matrix by using dim() . We assign dimension of matrix using dim() and create a matrix on “a” object.
a <- 1:20
We assign dimension as rows X columns .
dim(a) <- c(4,5) # number of rows = 4 , number of columns =5
We can transpose matrix by using t() .
This operator is used to multiply a matrix with its transpose.
We can bind matrices by row or column .
We can bind two matrices column-wise . When we bind columns, the number of rows of matrices should be same.
When the two matrix do not have same number of rows , it will join . There is an ERROR comes while binding them.
We can also bind vectors by using following code:
We can bind matrices by row-wise . When we bind rows , the number of columns of two matrices should be same.
We can also bind vectors row-wise as:
We can store data in more than two dimensions . If we create an array of dimension (2,3,4) then it creates 4 rectangular matrices each of 2 rows and 3 columns.
An array can create by using array() . We used dim to assign dimension of array.
Arrays are also recycled same as matrix . We create two vectors and input these vectors to an array to fill the elements of array.
vector1 <- c(5,9,3)
vector2 <- c(10,11,12,13,14,15,16)
result <- array(c(vector1,vector2),dim = c(3,3,2))
We can give names to columns , rows and matrices in the array by using dimnames parameter.
vector1 <- c(5,9,3)
vector2 <- c(10,11,12,13,14,15)
column.names <- c(“first”,”second”,”third”)
row.names <- c(“first”,”second”,”third”)
matrix.names <- c(“Matrix1″,”Matrix2”)
result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,column.names,
We can show the third row of the first matrix of the array .
We can show the element in the 1st row and 2nd column of the 1st matrix.
Check out second matrix .
Create matrices from the array .
We add two matrices also :
Factors are the data objects which are used to categorize the data and store it. They can store both strings and integers .
Factors are created using factor() function .
l <- c(“male”,”female”)
Levels shows all possible values of given object . We can check levels of object .
 “female” “male”
We create another factor variable Name :
 “1” “2”
To convert the default factor Name to roman numerals, we use the assignment form of the levels() function:
levels(Name) = c(‘I’,’II’)
It is used to build a contingency table of the count of each combination of factor variables .
mons = c(“March”,”April”,”January”,”November”,”January”,
mons = factor(mons)
Data Frame is a two dimensional data structure . The characteristics of a data frame are :
- The column names should be non-empty . Every column has assign certain name.
- The row names should be unique.
- The data can be of numeric , character or factor type.
- Each column should contain same number of elements.
We create a data frame name ” myFirstDataFrame ” as:
myFirstDataFrame <- data.frame(name = c(“Bob”, “Fred”, “Barb”, “Sue”,”Jeff”),
age = c(21,18,18,24,20),
race= c(“Cauc”, “Af.Am”,”Af.Am”, “Cauc”, “Asian”),
We can view data frame by :
We can find number of rows and columns by using nrow() and ncol() .
A list is a generic vector . It is combination of different objects .
We can create list as :
[] <- it shows first object from list
 1 2 3 4 <- it shows elements of first object
[] <- it shows second object from list
 “abc” <- it shows elements of second object
[] <- it shows third object from list
 TRUE <- it shows elements of third object
We can create a list by combining different objects as:
b<-c(“Alec”, “Dan”, “Rob”, “Rich”)
c <- c(TRUE, TRUE, FALSE, FALSE)
We create a list of integer , matrix and character as:
x <- 1:10
y <- matrix(1:12, nrow=3)
z <- “Hello”
mylist <- list(x,y,z)
We create a list contains character , matrix and list objects as:
list_data <- list(c(“January”,”February”,”March”), matrix(c(4,8,6,9,-5,3), nrow = 2),
We can give names to the list objects by using names() as:
names(list_data) <- c(“1st Quarter”, “A_Matrix”, “A Inner list”)