- library(dplyr)
- unite(mtcars, "vs_am", vs, am)
Merging Data
Adding Columns
To merge two data frames (datasets) horizontally, use the merge function. In most cases, you join two data frames by one or more common key variables (i.e., an inner join).
# merge two data frames by ID
total <- merge(data frameA,data frameB,by="ID") #by指定的列中的值必须是唯一的,不能重复出现两行有相同的ID
# merge two data frames by ID and Country
total <- merge(data frameA,data frameB,by=c("ID","Country")) #by指定的列中的值必须是唯一的,不能重复出现两行有相同的ID
Inner join: merge(df1, df2)
will work for these examples because R automatically joins the frames by common variable names, but you would most likely want