การจับคู่ที่ซ้ำกัน

การ Join ข้อมูลด้วย data.table ใน R

Scott Ritchie

Postdoctoral Researcher in Systems Genomics

คีย์การจอยน์ที่ซ้ำกัน

# Which bacteria could be found at both sites using any method?
site1_ecology[site2_ecology, on = .(genus)] 

การ Join ข้อมูลด้วย data.table ใน R

ข้อผิดพลาดจากการจับคู่แบบคูณ

site1_ecology[site2_ecology, on = .(genus)]

Error in vecseq(f__, len__, if (allow.cartesian || notjoin || 
!anyDuplicated(f__,  : 
  Join results in 12 rows; more than 10 = nrow(x)+nrow(i). Check for 
  duplicate key values in i each of which join to the same group in x over 
  and over again. If that's ok, try by=.EACHI to run j for each group to 
  avoid the large allocation. If you are sure you wish to proceed, rerun 
  with allow.cartesian=TRUE. Otherwise, please search for this error message 
  in the FAQ, Wiki, Stack Overflow and data.table issue tracker for advice.
การ Join ข้อมูลด้วย data.table ใน R

อนุญาตให้จับคู่แบบคูณ

allow.cartesian = TRUE อนุญาตให้ดำเนินการจอยน์ต่อได้:

# data.table syntax
site1_ecology[site2_ecology, on = .(genus), allow.cartesian = TRUE]
# merge()
merge(site1_ecology, site2_ecology, by = "genus", allow.cartesian = TRUE)
การ Join ข้อมูลด้วย data.table ใน R

อนุญาตให้จับคู่แบบคูณ

site1_ecology[site2_ecology, on = .(genus), allow.cartesian = TRUE]
           genus count method present i.method
 1: Nitrosomonas   500    WGS    TRUE      WGS
 2: Nitrosomonas   620    16S    TRUE      WGS
 3: Nitrosomonas   500    WGS    TRUE      16S
 4: Nitrosomonas   620    16S    TRUE      16S
 5: Nitrosomonas   500    WGS    TRUE  Culture
 6: Nitrosomonas   620    16S    TRUE  Culture
 7:    Rhizobium   360    WGS    TRUE      WGS
 8:    Rhizobium   300    16S    TRUE      WGS
 9:    Rhizobium   360    WGS    TRUE      16S
10:    Rhizobium   300    16S    TRUE      16S
11:    Rhizobium   360    WGS   FALSE  Culture
12:    Rhizobium   300    16S   FALSE  Culture
การ Join ข้อมูลด้วย data.table ใน R

ค่าที่หายไป

ค่าที่หายไป (NA) จะจับคู่กับค่าที่หายไปอื่น ๆ ทั้งหมด:

การ Join ข้อมูลด้วย data.table ใน R

กรองค่าที่หายไป

!is.na() ใช้กรองแถวที่มีค่าที่หายไปได้

site1_ecology <- site1_ecology[!is.na(genus)]
site1_ecology
          genus count method
1: Nitrosomonas   500    WGS
2:    Rhizobium   360    WGS
site2_ecology <- site2_ecology[!is.na(genus)]
site2_ecology
          genus present  method
1: Nitrosomonas    TRUE Culture
2:    Rhizobium    TRUE Culture
3:  Azotobacter    TRUE Culture
การ Join ข้อมูลด้วย data.table ใน R

เก็บเฉพาะการจับคู่แรก

site1_ecology[site2_ecology, on = .(genus), mult = "first"]

การ Join ข้อมูลด้วย data.table ใน R

เก็บเฉพาะการจับคู่สุดท้าย

children[parents, on = .(parent = name), mult = "last"]

การ Join ข้อมูลด้วย data.table ใน R

การระบุและลบข้อมูลที่ซ้ำกัน

duplicated(): ตรวจสอบว่าแถวใดซ้ำกัน

unique(): กรอง data.table ให้เหลือเฉพาะแถวที่ไม่ซ้ำ

การ Join ข้อมูลด้วย data.table ใน R

ฟังก์ชัน duplicated()

ใช้ค่าจากทุกคอลัมน์:

duplicated(site1_ecology)
FALSE FALSE FALSE FALSE

ใช้ค่าจากคอลัมน์ที่เลือก:

duplicated(site1_ecology, 
           by = "genus")
FALSE TRUE FALSE TRUE

การ Join ข้อมูลด้วย data.table ใน R

ฟังก์ชัน unique()

unique(site1_ecology, by = "genus")

การ Join ข้อมูลด้วย data.table ใน R

เปลี่ยนลำดับการค้นหา

fromLast = TRUE เปลี่ยนทิศทางการค้นหาให้เริ่มจากแถวสุดท้าย

duplicated(site1_ecology, by = "genus", fromLast = TRUE)
TRUE FALSE TRUE FALSE
unique(site1_ecology, by = "genus", fromLast = TRUE)

การ Join ข้อมูลด้วย data.table ใน R

มาฝึกกันเถอะ!

การ Join ข้อมูลด้วย data.table ใน R

Preparing Video For Download...