Free Shipping

Secure Payment

easy returns

24/7 support

  • Home
  • Blog
  • Introduction to Data Structures in R

Introduction to Data Structures in R

 July 13  | 0 Comments

While you do programming in any programming language, you need to use various variables to store different data. Variables are however, reserved in a memory locations to store values. This implies that, once you create a variable you reserve some area in memory. Data Structures are the way of arranging data so that it can be used efficiently in a computer.
In contrast to different programming languages like C and Java, R doesn’t have variables declared as some data type. The variables are appointed with R-Objects and the knowledge form of the R-object becomes the datatype of the variable. There are many types of R-objects. The popularly used ones are

  • Vector
  • Matrix
  • Array
  • Lists
  • DataFrames

The very first type Vector we are covering in detail is present in Beginners Guide to R.
We will follow the rest of the data types.

R Data Structures: Matrix

A matrix is a vector with two attributes
• Number of rows.
• Number of columns
Matrices can be defined as 2-dimensional arrays. All columns in a matrix must have the same mode (numeric, character, etc.) and same length. A matrix can be created by using the matrix() function:
myymatrix <- matrix(vector, nrow=number_of_rows, ncol=number_of_columns, byrow=logical_value, dimnames=list( char_vector_rownames, char_vector_colnames))
Y <-matrix(c(1,2,3,4),nrow=2,ncol=2)

We can show the name of matrix by class function and the attributes will show the dimension for matrix.

class(matrix name)

attributes(matrix name)


Transpose of a matrix is to convert row into column and column into row.

variable = t(matrix name)


Here we see that the matrix y has been transposed.
Trans = t(y)
print(Trns)Naming rows of matrix
We employ the function rownames() to change names of rows
For the columns, we want to supply a vector of different names for the four subs involved in the test, and use this to specify the colnames(X):
subs<-c(“Maths”, “English”, “Science”, “History”) colnames(X)<-subs XCalculations on Rows Or Columns Of The Matrix
We can use subscripts to select parts of the matrix, with a blank meaning �?all of the rows’ or �?all the columns’. Here is the mean of the rightmost column (number 4)
mean(x[,4])calculated over all the rows (blank then comma), and the standard deviation of the bottom row,
sd(x[3,])There are some special functions for calculating summary statistics on matrices:
colMeans(x)Adding Rows and Columns to The Matrix
Add a row at the bottom showing the column means, and a column at the right showing the row variances:
XEvaluating Functions with Apply
The apply function is used for applying functions to the rows or columns of matrices or data frames. For example:
y<-matrix(1:16,nrow=4)Often, you want to apply a function across one of the margins of a matrix – margin 1 being the rows and margin 2 being the columns. Here are the total row (four of them):
apply(y,1,sum)Here are the total columns (four of them):
The sweep function is used to �?sweep out’ array summaries from vectors, matrices, arrays or data frames.
In this example, we want to express a matrix in terms of the departures of each value from its column mean.
matdata<- matrix(c(50,60,40,90,100, 80,50, 90,10, 80,30, 70),nrow=4)
Create a vector containing the parameters that you intend to sweep out of the matrix. Let’s say we want to compute the four column means:
Now it is straightforward to express all the data in matdata as departures from the relevant column means: