In: Computer Science
Which of the following is TRUE about horizontal data merges using R? (Multiple answers can be selected)
a) Both data frames must have the same number of columns
b) Both data frames must have the same number of rows
c) The data frames must have at least one column with values in common
d) The data frames must have at least one column with the same name
e) Both data frames must have the same order of columns from left to right
A horizontal data merge combines data frames horizontally, i.e it adds columns to an existing data frame with the help of common column data(Ex: ID). It is only possible if both the data frames have the same set of observations. It basically adds columns to the existing data frame. In order to do that, both the data frames must have the same number of rows and the order of observations should be the same. If the rows are ordered inconsistently then the horizontal merge does not make any sense. So by making use of this data, lets answer the given question.
a.) Both data frames must have the same number of columns. - FALSE.
Having the same number of columns is not necessary but the order of observations is what makes it more important.
b.) Both data frames must have the same number of rows. - TRUE.
Because without the same number of rows two data frames cannot be merged horizontally.
c.) The data frames must have at least one column with values in common. - TRUE
Because the order of observations is important to merge data frames horizontally. So there should be at least one column shares the common values between them in order to merge. If there is no such common column, the R cannot understand how to merge.
d.) The data frames must have at least one column with the same name. - TRUE
This is identically the same as above. There should be at least one column with the same name to merge. If there is no such common column with the same name, the R cannot understand how to merge.
e.) Both data frames must have the same order of columns from left to right - FALSE.
Having the same order of columns from left to right is not necessary for merging data frames horizontally. But, that is a necessary condition for merging vertically.
The following is an example of merging data frames horizontally.
Data Frame 1
ID | Name | Score |
1 | Mohan | 98 |
2 | Abhi | 99 |
3 | Suresh | 87 |
Data Frame 2
ID | Test1 | Test2 | Test3 |
1 | 98 | 98 | 98 |
2 | 100 | 98 | 100 |
3 | 86 | 88 | 87 |
After Merging horizontally.
ID | Name | Score | Test1 | Test2 | Test3 |
1 | Mohan | 98 | 98 | 98 | 98 |
2 | Abhi | 99 | 100 | 98 | 100 |
3 | Suresh | 87 | 86 | 88 | 87 |
#Please don't forget to upvote if you find the solution
helpful. Feel free to ask doubts if any, in the comments section.
Thank you.