Data structures in programming languages are used to store data in an organized manner. Data structures in R are similarly used to store data for different purposes. Basic data structures in R are ,

  • Vectors
  • Arrays
  • Data frames
  • Matrices
  • Factors
  • Lists

Let’s talk about each data structure briefly.


Vectors are one-dimensional arrays. c() function is used to create a vector.

  • nums <- c(1,2,3,4,6,-2,0,13) — — a numeric vector
  • words <-c(“one”,”two”,”apple”) — — a character vector
  • status <- c(TRUE,FALSE,TRUE,FALSE) — — a logical vector


Scalars are one element vectors which means scalars hold constant values.

  • num <- 9 — — a numeric scalar
  • wor <-”abc” — — a character scalar
  • stt <-TRUE — — a logical scalar

Colon operator

Colon operator is used to create a vector with a sequence of numbers.

x<- c(1:10) — — equivalent to x<- c(1,2,3,4,5,6,7,8,9,10)

Accessing vector elements

Accessing vector elements is almost Similar to python. But unlike in python, indexing in vector elements in R starts with 1.

Try the example :

Consider the vector vect <- c(“a”,”b”,”c”,”d”,”e”)

  • vect[3] => returns the third element ”c” of vect
  • vect[1] => returns the first element ”a” of vect
  • vect[c(1,5)] => return first and fifth elements of vect as “a” ”e”

2. Matrices

Matrices are two-dimensional data structures. Elements in a matrix must be of the same data type. matrix() function is used to create matrices.

Try the example :

  • vect <-c(1,2,3,4,5,6)
  • mat <- matrix(vect, nrow=2, ncol=2)

matrix() function makes the vector into a grid according to the number of rows and columns given.

Parameter byrow in matrix function allows you to interchange configuration of the matrix from column format to row format.

Accessing Matrix elements

Consider the matrix vect <-c(1,2,3,4) and mat<-matrix( vect ,nrow=2,ncol=2)

Try the example :

  • mat [1,] => returns the first row of the matrix
  • mat[,1] => returns the first column of the matrix
  • mat [1,2] => returns element in the first row of the second column

3. Arrays

Arrays are almost similar to matrices. But arrays may have more than 2 dimensions. array() function is used to create arrays.

Try the example :

  • vect_1<-c( 1, 2, 3 )
  • vect_2<-c( 2, 4, 6, 8, 10, 12)
  • arr<-array(array(c(vect_1,vect_2),dim=c(3,3,2)))

Accessing Array Elements

Try the example :

  • arr[2,,] => return all elements in the second dimension
  • arr[2,1] => returns the first row element in the second dimension

4. Data frames

Data frames are the most popular data structure in R since it is the commonly used. Data frames are similar to matrices, but data frames may contain different data types in its columns. data.frame() function is used to create data frames.

Try the example :

  • name<-c(“Kate”,”Sam”,”Patty”,”Mary”)
  • married<-c(TRUE,TRUE,FALSE,FALSE)
  • age<-c(25,30,12,18)
  • frm<-data.frame(name,married,age)

Accessing Data Frame Elements

Try the example :

  • frm<-data.frame(name,married,age)
  • frm$married -> return the married vector in frm
  • frm$name[1] -> return the first element of name vector in frm

5. Factor

Factors are a special type of data structures in R. These are categorical variables. factor() function is used to return levels in a vector.

Try the example :

  • status <- c(“yes”, “no”, “no”, “yes”)
  • f<- factor(status)

6. Lists

Lists in R may contain a combination of vectors, matrices, data frames. It may also contain other lists. It may also contain columns of data with different data types.

Try the example :

  • vect_1<-c(1,2,3,4)
  • vect_2 <- c( ‘a’,’b’,’c’,’d’ )
  • matr<-matrix( vect_2, nrow= 2, ncol= 2)
  • lst<-list( vect_1 ,matr )

References :


[2] RStudio > help

Computer Science and Engineering undergraduate