How do I combine two data-frames based on two columns?
本问题已经有最佳答案,请猛点这里访问。
我知道我可以用
看到的文献
1 2 | By default the data frames are merged on the columns with names they both have, but separate specifications of the columns can be given by by.x and by.y. |
这将是明确implies
1 2 3 | x <- data.frame(k1=c(NA,NA,3,4,5), k2=c(1,NA,NA,4,5), data=1:5) y <- data.frame(k1=c(NA,2,NA,4,5), k2=c(NA,NA,3,4,5), data=1:5) merge(x, y, by=c("k1","k2")) # NA's match |
这个实例是可能的demonstrate使用
希望这helps; </P >
1 2 3 4 5 6 7 8 | df1 = data.frame(CustomerId=c(1:10), Hobby = c(rep("sing", 4), rep("pingpong", 3), rep("hiking", 3)), Product=c(rep("Toaster",3),rep("Phone", 2), rep("Radio",3), rep("Stereo", 2))) df2 = data.frame(CustomerId=c(2,4,6, 8, 10),State=c(rep("Alabama",2),rep("Ohio",1), rep("Cal", 2)), like=c("sing", 'hiking',"pingpong", 'hiking',"sing")) df3 = merge(df1, df2, by.x=c("CustomerId","Hobby"), by.y=c("CustomerId","like")) |
assuming
你也可以使用我的命令(dplyr)。 </P >
例如: </P >
1 | new_dataset <- dataset1 %>% right_join(dataset2, by=c("column1","column2")) |