Vector, Matrix, Dataframe, List: The basic data structures in R

Sharing buttons:

friends in this video you would learn

what kind of data structures are there

in our studio and how could we use them

for our purpose

so first the simplest object in our is


I had already discussed this in my

previous video but like I am describing

it again here we can generate a vector a

vector is like a column of a matrix or

like a data frame it's like a variable

in our data we use in our we can

generate a vector using C command

suppose you want to generate a character

vector ROM and Chomp we can use it x

equals the assignment operator here in

this way similarly for names and

similarly you cannot generate a numeric

vector as well suppose I say ax by these

three vectors I had generated here I

have generated here first two are

corrector vectors third one is numeric

vector now we come to the next object

that is the matrix matrix is a two

dimensional array data structure it

shows all the elements of a matrix must

be of the same type like they should be

either numerical logical or character

and you can make a matrix by using

matrix come on but one thing we should

keep in mind that all the vectors or all

the columns of a matrix should be of

same data-type and of same data length

suppose I want to generate a matrix of

like sequence one to ten I have ten

observations like a sequence from one to

ten so I can generate that sequence and

I can use the matrix command matrix the

sequence and number of rows and columns

so I'm saying number of rows should be

equal five and number of columns should

be equal to two I can use it so I have

generated a matrix a if I click on a

here I can see it's a matrix that has

two vectors vector birth and vector two

here vector two values are one two five

and six to ten we can also verify by

using a command is dot matrix whether

this matrix a is a matrix or not what is

the dimension by using dimke man by

using dim command we can identify the

dimension like number of rows and


Andrew will give me the number of rows

and call will give me the number of

columns length will give me the total

number of observations in a matrix I run

this command dimension PI by 2 number of

rows by number of columns 2 and length

is 10 5 by 2 10 so why do we use matrix

in our at all first things friends

whenever we have numeric data and we

want to conduct some operations like we

want to make loops we want to do

simulations we want to do matrix

operations in that situation matrix is

more easy and more fast data structure

that could be performed very easily and

in a fast manner in our and now we come

to one more structure that is data frame

matrix and data frame are very similar

objects the difference lie here all the

elements in a matrix should be of same

data-type but here in data frame because

it contains the features of both matrix

and list here what we can do a data

frame is like list of equivalent vectors

of different data types like I had shown

earlier all the variables all the like

all the columns in a matrix there should

be of same data-type but here when we

say data frame they could be of

different type suppose X Y vector was a

character vector and Y x1 was a vector

of character vector and why was the

numerical vector if I want to make a

data frame by using these two vectors

what'll I do I am generating a vector

data frame a using data frame command


odd frame and just combining these two

vectors of different types but same

length for food we have four elements in

X 1 we have four elements in by I just

click run here now I have a data frame a

which contains two vectors X 1 and X 2 4

is the length that is same but data type

it's a character

it's a numeric different okay friend so

I can see here matrix contains all the

columns of same data-type but same

length but a data frame could be of

different data types but there should be

of same length okay friends we can

verify by using a store data frame it's

true yes it is dimension the same

function as we used heat they they're

the names of columns in data frame x1

and by number of rows by and row number

of column but when you slant command

here it will give us the number of

columns not total number of observations

as we had in matrix

it's two here occurrence data frame is

the highly used structure in our because

most of the data we have in our research

whether it is PhD masters or any

discipline the data is in data frame

object because we have like age data

from numeric arrays also there are some

categorical data is also there so we use

data frames mostly now we move one more

thing friends here the columns of a data

frame could be accessed by their name

suppose I say we have data frame data

frame e okay if we put dollar we can see

a list of all the columns it has X word

byte so suppose I want to access Y and

we have these elements by 6 9 4 in Y if

you want to access so we can access the

columns by their name using dollar sign


front of the name of data frame now we

move to the highest object list list is

the most powerful structure for looping

they can contain any data type of

different lengths as well okay friends

here the main benefit is in data frame

what we had all the data type we have in

a data frame there should be of same

length but here there could be of

different lengths as well so here

suppose we want to make a list a we want

to make a list a that has three vectors

X X 1 and Y X has two elements X one has

four Y has four I add all of them by

using list command I can verify also it

is doubtless yes it's true and suppose

we want to access the elements on all

the components here we can use these

double square brackets suppose I want to

go the first x1 the X vector so how

should I do list a double square

brackets and component number one the

first component here tom and charm and

suppose I want to extract the first

element of first component I can use

double square one

it would take me to the first variable

then to the first element by this single

square in this way similarly for the

third one and the fourth of third

component for okay friends in this way

so these are the basic data types data

structures we use in our it's a vector

combination of equal length vector is a

matrix but or the data type should be

same and when we moved data frame it is

a combination of district of the columns

of different data types

of equal length in lists we can have

different columns of different types of

different lengths as well so thank you

friends keep watching