简体   繁体   中英

merge or left join R

I have three datasets that has same Index column (Key), StudentId, and all have same number of observations, I repeat same number of observations, but different columns and different values for each observation.

    Dataset 1
    Id     Lab 
     1      33     
     .      .
     2334   98

    Dataset 2
    Id     Sports
     1      83     
     .      .
     2334   933

    Dataset 3
    Id     Lunch
     1      33     
     .      .
     2334   238

I dont know why when I use merge or left_merge to combine all the three datasets the final dataset shows up more number of rows than it should man ?

    Dataset Final
    Id     Sports   Lunch     Lab
     1      33       83       33 
     .      .
     3564   98       34       56

如果您对所有3个数据集的Id顺序相同,则只需使用:

res <- cbind(d1,d2,d3)

It's hard to tell without a reproducible example, but my best guess would be that the values of your Id variable are slightly different in your data sets. If you're working with dplyr (what I assume given that you use left_join ), you might instead use inner_join() which merges only rows that are included in both data sets. (However, it's likely that your final data frame will have less rows than your three data frames that you use for merging.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM