【问题标题】:Best method to Merge two Datasets (Maybe if function?)合并两个数据集的最佳方法(也许是函数?)
【发布时间】:2017-12-08 17:51:50
【问题描述】:

我正在使用两个数据集。数据集 TestA 和 Test B(下面是如何在 R 中制作它们)

Instructor <- c('Mr.A','Mr.A','Mr.B', 'Mr.C', 'Mr.D')
Class <- c('French','French','English', 'Math', 'Geometry')
Section <- c('1','2','3','5','5')
Time <- c('9:00-10:00','10:00-11:00','9:00-10:00','9:00-10:00','10:00-11:00')
Date <- c('MWF','MWF','TR','TR','MWF')
Enrollment <- c('30','40','24','29','40')

TestA <- data.frame(Instructor,Class,Section,Time,Date,Enrollment)

rm(Instructor,Class,Section,Time,Date,Enrollment)

Student <- c("Frances","Cass","Fern","Pat","Peter","Kory","Cole")
ID <- c('123','121','101','151','456','789','314')
Instructor <- c('','','','','','','')
Time <- c('','','','','','','')
Date <- c('','','','','','','')
Enrollment <- c('','','','','','','')
Class <- c('French','French','French','French','English', 'Math', 'Geometry')
Section <- c('1','1','2','2','3','5','5')


TestB <- data.frame(Student, ID, Instructor, Class, Section, Time, Date, Enrollment)

rm(Instructor,Class,Section,Time,Date,Enrollment,ID,Student)

我想合并两个数据集(如果可能,不使用 merge() )以便测试 A 的所有列都填充了测试 B 提供的信息,并且应该根据类和部分添加它。

我尝试使用 merge(TestA, TestB, by=c('Class','Section'), all.x=TRUE) 但它向原始 TestA 添加了观察结果。这只是一个测试,但在我使用的数据集中有数百个观察结果。当我使用这些较小的框架进行操作时,它起作用了,但是更大的集合发生了一些事情。这就是为什么我想知道是否有合并替代方案。

关于如何做到这一点的任何想法?

输出应该是这样的

Class   Section Instructor  Time          Date   Enrollment Student ID
English  3      Mr.B    9:00-10:00  TR      24      Peter   456
French   1      Mr.A    9:00-10:00  MWF     30      Frances 123
French   1      Mr.A    9:00-10:00  MWF 30  Cass    121
French   2      Mr.A    10:00-11:00 MWF 40  Fern    101
French   2      Mr.A    10:00-11:00 MWF 40  Pat 151
Geometry 5      Mr.D    10:00-11:00 MWF 40  Cole    314
Math     5      Mr.C    9:00-10:00  TR  29  Kory    789

【问题讨论】:

  • 你能举例说明你期望的输出是什么吗?
  • 您是否只想合并来自TestB 的特定列集,例如merge(TestA, TestB[, c("Class","Section","ID")], by = c("Class", "Section"), all.x = T)
  • 在这种情况下,TestB 的所有列都在 TestA 中,所以我希望它们全部合并,但按类和部分合并它们
  • 我添加了输出应该是@SymbolixAU

标签: r


【解决方案1】:

我曾经是merge() 的忠实粉丝,直到我了解了dplyrjoin 函数。

试试这个:

library(dplyr)

TestA %>%
    left_join(TestB, by = c("Class", "Section")) %>% #Here, you're joining by just the "Class" and "Section" columns of TestA and TestB
    select(Class, 
           Section, 
           Instructor = Instructor.x, 
           Time = Time.x, 
           Date = Date.x, 
           Enrollment = Enrollment.x, 
           Student, 
           ID) %>%
    arrange(Class, Section) #Added to match your output.

select 语句只保留那些特别命名的列,在某些情况下,还会重命名它们。

输出:

     Class Section Instructor        Time Date Enrollment Student  ID
1  English       3       Mr.B  9:00-10:00   TR         24   Peter 456
2   French       1       Mr.A  9:00-10:00  MWF         30 Frances 123
3   French       1       Mr.A  9:00-10:00  MWF         30    Cass 121
4   French       2       Mr.A 10:00-11:00  MWF         40    Fern 101
5   French       2       Mr.A 10:00-11:00  MWF         40     Pat 151
6 Geometry       5       Mr.D 10:00-11:00  MWF         40    Cole 314
7     Math       5       Mr.C  9:00-10:00   TR         29    Kory 789

【讨论】:

  • @LordVoldemort - 虽然这是正确的 (+1),但它似乎比我评论中的解决方案更复杂:merge(TestA, TestB[, c("Class","Section","ID", "Student")], by = c("Class", "Section"), all.x = T)
  • @SymbolixAU 毫无疑问,您的解决方案要简单得多,但 OP 询问如何“不使用 merge()”合并两个数据集。
【解决方案2】:

关键是从TestB 中删除空但重复的列合并/加入之前,如SymbolixAU 所示。

这是data.table 语法的实现:

library(data.table)
setDT(TestB)[, .(Student, ID, Class, Section)][setDT(TestA), on = .(Class, Section)]

   Student  ID    Class Section Instructor        Time Date Enrollment
1: Frances 123   French       1       Mr.A  9:00-10:00  MWF         30
2:    Cass 121   French       1       Mr.A  9:00-10:00  MWF         30
3:    Fern 101   French       2       Mr.A 10:00-11:00  MWF         40
4:     Pat 151   French       2       Mr.A 10:00-11:00  MWF         40
5:   Peter 456  English       3       Mr.B  9:00-10:00   TR         24
6:    Kory 789     Math       5       Mr.C  9:00-10:00   TR         29
7:    Cole 314 Geometry       5       Mr.D 10:00-11:00  MWF         40

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-13
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 2016-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多