Data structure types

R provides numerous ways to hold data. Let’s check some of them.

Vectors

A vector is a collection of values. We call the individual values “elements” of the vector.

We can make vectors with c( ). c means “combine”. Say you have three numbers 1,4,5 and you just do not want to type them every time. you can then put the numbers in a vector.

c(1,4,5) #here I have a vector with three numbers
## [1] 1 4 5

You can also run operations in a vector. Lets use the example above, in which I want to multiply each value in that vector by 2.

a=c(1,4,5) #here create a variable that contains my vector of three numbers
a*2        #here I multiply my variable (vector with three numbers)  by 2.
## [1]  2  8 10

Matrix

You can merge multiple vectors to create a matrix. You can merge the vectors by columns using the function cbind or by rows using the function rbind. Lets do an example to clarify.

Vector1=c(1,4,5)                          #here I have vector 1 with three numbers
Vector2=c(56,42,93)                       #here I have vector 2 with three numbers
ColumnMergedVector=cbind(Vector1,Vector2) #I use cbind to merge the vector by column
ColumnMergedVector
##      Vector1 Vector2
## [1,]       1      56
## [2,]       4      42
## [3,]       5      93

See in the result above, how each vector became a column.

May be you prefer to merge them by rows

RowMergedVector=rbind(Vector1,Vector2) #I use rbind to merge the vector by rows
RowMergedVector
##         [,1] [,2] [,3]
## Vector1    1    4    5
## Vector2   56   42   93

You can now see how the two vectors were merged by rows.

DataFrames

Dataframes are the most common type of data structure in R. It is has similar topology to a matrix, but it is more diverse in the types of data it can use. Dataframes, are like the “Sheet” version in Excel. To create a dataframe you use the data.frame function. Lets do an example.

s

DataFrame=data.frame(Vector1,Vector2) #I use data.frame to merge two vectros of data
DataFrame
##   Vector1 Vector2
## 1       1      56
## 2       4      42
## 3       5      93

Dataframes also allow you to rename the column and row names, and there different ways to do that. Here is one,

colnames(DataFrame)=c("Height","Width") #Here I rename the two columsn in the dataframe, with the names used in a vector. 
DataFrame
##   Height Width
## 1      1    56
## 2      4    42
## 3      5    93

You are probably asking yourself what is the difference between a matrix and a data.frame?. Here are some differences:

Matrix Dataframe
Collection of data sets arranged in a two dimensional rectangular organisation Stores data tables that contains multiple data types in multiple column called fields.
It’s m*n array with similar data type. It is a list of vector of equal length. It is a generalized form of matrix.
It has fixed number of rows and columns. It has variable number of rows and columns.
The data stored in columns can be only of same data type. The data stored must be numeric, character or factor type.
Data types are homogeneous. Data types are heterogeneous.