Lecture 44

IIT Kanpur July 201
20 followers
Oct 25, 2023
Hello friends, welcome to the course of Foundation of R Software.
You might remember that in the last few lectures, we have discussed about DataFrame and tried to do many operations.
Today, we will try to learn a new operation in the same DataFrame.
That is, how to combine our different DataFrames.
This is a very common condition.
If many people are working on a project and they have given their data in DataFrame format.
We want to combine all the data together and keep it in the same file.
Today, we will learn how to combine DataFrame and DataFrame2.
Now the question is, how can we combine our DataFrame and DataFrame2?
Suppose we have two DataFrames, DataFrame1 and DataFrame2.
The first option is to keep DataFrame1 and DataFrame2 side by side.
The second option is to keep DataFrame1 and DataFrame2 side by side.
The third condition is that there is a common column in the two DataFrames.
We want to combine both the DataFrame and DataFrame2 side by side.
But the common column in both the DataFrame and DataFrame2 side should be in the same place.
So, how can we do this?
Today, we will learn how to combine DataFrame and DataFrame2 side by side.
But before moving ahead, I would like to tell you that
today we will come up with the topic of DataFrame.
But this does not mean that we have learned all the commands and functions of DataFrame.
I have just tried to tell you that
the way you work in a spreadsheet or excel file, you can do the same thing in R software.
And I have tried to tell you about some of our common operations.
But apart from that, we have many other operations that we do to handle our data management.
So, I have not covered all the operations here.
Because if I cover all the operations, then we will only be able to talk about DataFrame in the entire course.
So, for that, I will depend on you.
And I will expect you to try to do the same operations in R software.
If you are handling an excel file or a spreadsheet,
then you can try to do the same operations in DataFrame.
So, let's talk about how to combine DataFrame.
And let's start this lecture.
So, if we try to understand how to combine DataFrame,
then you assume that we have two DataFrames.
One is DF1 and the other is DF2.
Now, if I want to combine them, then the first option is to place both DataFrames side by side.
Like this, DF1 and DF2.
That is, we combine both DataFrames horizontally.
And the second option is to place both DataFrames on top of each other.
Like we say this is DF1 and DF2.
And another option is that if there is a common column in DF1 and DF2,
then we place that common column in both DataFrames at the same place.
And from here, we combine DF1 and DF2.
So that the column in the middle remains common.
To merge these three types, we have three commands here.
The first command is cbind.
So, this function helps us to merge two DataFrames column-wise side by side.
That is, both DataFrames will merge side by side with the help of our column.
The second command is merge.
With this command, if there is a common column in both DataFrames,
then it will keep the common column of both DataFrames in mind.
And it will write it at the same place.
And the rest of the DataFrame will merge it horizontally.
As I have shown you here.
And the third option is rbind.
In this, it keeps two DataFrames on top of each other and merges them.
As I have shown you here.
So, cbind, merge and rbind are all written in lowercase alphabets.
And in parentheses, we write the name of the DataFrame.
And we try to write the different options here.
So, let's try to understand these three commands one by one.
So, first of all, let's talk about cbind.
Everything will be written in lowercase alphabets.
So, this cbind function tries to merge two DataFrames horizontally side by side.
So, let's work to understand it.
So, let's create two DataFrames.
And I have created this DataFrame in a very easy way so that you can understand it.
So, I have taken one DataFrame and I have written a command to create it.
data.frame
And from this, I have taken two data vectors, two variables.
One is state.
And it has four values.
up, mp, ap, jk
Uttar Pradesh, Madhya Pradesh, Andhra Pradesh, Jammu Kashmir
And the population size there, I have written it in the next variable.
popnsize
And in this, I have written a value of 1000, 2000, 3000, 4000.
That is, the population size of UP is 1000, 2000 of MP, 3000 of Andhra Pradesh, and 4000 of Jammu Kashmir.
These are all hypothetical values.
Just to make you understand.
And I have named this DataFrame df1.
Similarly, I have created another DataFrame df2.
And I have written data.frame
And in this too, I have taken three variables.
One is state.
Which is up, mp, ap, jk.
This is from the previous method.
And with that, I have taken two more variables.
sampleSize
So, this is 100, 200, 300, 400.
And after that, I have taken one more vector.
Where it is written that the survey completed.
And the answer is yes, no, yes, no.
In the form, this is our character string.
So, this is how our data is.
If we talk about both.
So, in the first case, we have only two variables.
One is our state.
And the other is our population size.
Right.
And see here, our values are given.
And in the same way, we have another data set, df2.
So, now if you see, this df1 and df2 will be shown to you in this way.
As I told you.
See here, there are two empty columns.
state and population size
In df1 and df2, you have three columns.
state, sampleSize and surveyCompleted.
Right.
Now, we have to merge these two together.
So, I used the command here.
cbind
I wrote cbind df1, df2
So, now if you pay attention.
So, see, this is our horizontally merged.
And if you see this part of it.
So, what is this for you? df1
And the part you will see, what is this for you?
What is this for you? df2
And here he did not say anything about it.
That this is common in both of them.
So, I have to write it in one place.
He just copied and pasted both of them as such.
And if you want to see again.
So, see, this was your df1.
And this was your df2.
And see here, you have joined them together.
Right.
So, let's do this operation in our software.
And also you see, what kind of screenshot you will see.
See, this is your df1.
And this is your df2.
And when you combined here.
So, see, this is your df1.
And this is your df2.
Right.
So, see, here it is merged.
Now, let's do it in our software.
So, that you will get more confidence.
That how it is working for us.
Right.
So, see, let's copy and paste this command here.
So, our df1 came like this.
And our df2 came like this.
After this, see, I bind it.
df1, df2.
And see, what came to you.
See, this is your state of operations.
See, it was copied here.
And this was state sample size survey completed.
See, it was copied here.
So, you see, this is how it has combined it completely.
Right.
So, you see, this was a very easy task.
It is not a very difficult task to do.
After that, now we take our next option.
This is ours that.
Any of our two data frames.
They have to be merged horizontally.
But the condition is that.
There is a column or row in between them.
Which is common.
Right.
So,
merge command.
This is our two data frames.
Horizontally combine it.
and
Which is among our common columns.
These are common row names.
It will consider them together.
We have options for that.
Right.
So, see, here I am.
To show you this merge command.
I have considered the same df1 and df2 data frame here.
So in df1 we have state and population size, in df2 we have state, sample size and survey
computed which I just took df1 and df2, right.
Now if you pay attention to both of them, see here state is common in both, so we want
that our data merge should be in such a way that state should be common in both, right.
So to do this we have command merge, m-e-r-g-e, merge and after that we will give our data
frame here and after that we have many options, we try to give them, like I said y, y.x, y.y,
so we can specify these and tell which column is common and should be used for this merge,
right.
So here we have one more option, sort, after merging our values will be sorted, this is
our logical variable which takes the value of true and false, in the same way we have
one more command no.dups, so this is also our logical variable which takes the value
of true and false and it handles the duplicated values in it.
Anyway, I want to keep my discussion very simple and easy, so I will use only one option
here, y.
So see this was your df1 and this is your df2, in df1 you have column name as state
and population size and in df2 you have state, sample size and survey completed, so you
will see that this state is common in both.
So to combine both horizontally, I have written here merge, m-e-r-g-e in lower case alphabets
and in parenthesis I have written df1, df2 and along with that I have written y is equal
to state and I have given state in double quotes, see this state has a different name,
now after this when you see its outcome, you will see like this, see state came in only
one place, ap, jk, mp, up and it has automatically sorted it and after this you will see that
this population size has come from df1 and sample size and survey completed has come
from df2, so you have seen that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
frames and along with that it has combined both data frames and along with that it has
combined both data frames and along with that it has combined both data frames and along
with that it has combined both data frames and along with that it has combined both data
