Merging data in R involves combining data from two different data frames based on one or more common columns. This can be done using base R functions, such as merge()
, or using more efficient and user-friendly functions from the tidyverse
package.
Here is an example of merging two data frames in R using tidyverse
:
main.r282 chars12 lines
In the example above, we create two data frames df1
and df2
with a common column id
. We then use the left_join()
function to merge df1
and df2
based on the id
column. The resulting merged data frame contains all the rows from df1
and matching rows from df2
, with NA
values in the value2
column for non-matching rows.
Other useful functions for merging data in R include inner_join()
, right_join()
, and full_join()
, which correspond to different types of SQL joins. Additionally, dplyr
functions such as mutate()
, filter()
, and group_by()
can be used to manipulate merged data frames further.
gistlibby LogSnag