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**

**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:

Syntax:

myymatrix <- matrix(vector, nrow=number_of_rows, ncol=number_of_columns, byrow=logical_value, dimnames=list( char_vector_rownames, char_vector_colnames))

Example:

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.

Syntax:

class(matrix name)

attributes(matrix name)

Example:

class(y)

attributes(y)

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

Syntax:

variable = t(matrix name)

print(variable)

Here we see that the matrix y has been transposed.

Example:

Trans = t(y)

print(Trns)**Naming rows of matrix**

We employ the function rownames() to change names of rows

And

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):

Example:

subs<-c(“Maths”, “English”, “Science”, “History”) colnames(X)<-subs X**Calculations 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)

Example:

mean(x[,4])calculated over all the rows (blank then comma), and the standard deviation of the bottom row,

Example:

sd(x[3,])There are some special functions for calculating summary statistics on matrices:

Example:

rowSums(x)

colSums(x)

rowMeans(x)

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:

Example:

x<-rbind(x,apply(x,2,mean))

x<-cbind(x,apply(x,1,var))

X**Evaluating Functions with Apply**

The apply function is used for applying functions to the rows or columns of matrices or data frames. For example:

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):

Example:

apply(y,1,sum)Here are the total columns (four of them):

Example:

apply(y,2,sum)**Sweep**

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.

Example:

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:

Example:

cols<-apply(matdata,2,mean)

Now it is straightforward to express all the data in matdata as departures from the relevant column means:

Example:

sweep(matdata,2,cols)