Khớp trùng lặp

Kết hợp dữ liệu với data.table trong R

Scott Ritchie

Postdoctoral Researcher in Systems Genomics

Trùng lặp ở khóa nối

# Những vi khuẩn nào xuất hiện ở cả hai địa điểm bằng bất kỳ phương pháp nào?
site1_ecology[site2_ecology, on = .(genus)] 

Kết hợp dữ liệu với data.table trong R

Lỗi do khớp nhân bội

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.
Kết hợp dữ liệu với data.table trong R

Cho phép khớp nhân bội

allow.cartesian = TRUE cho phép tiếp tục phép nối:

# Cú pháp data.table
site1_ecology[site2_ecology, on = .(genus), allow.cartesian = TRUE]
# merge()
merge(site1_ecology, site2_ecology, by = "genus", allow.cartesian = TRUE)
Kết hợp dữ liệu với data.table trong R

Cho phép khớp nhân bội

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
Kết hợp dữ liệu với data.table trong R

Giá trị thiếu

Giá trị thiếu (NA) sẽ khớp với mọi giá trị thiếu khác:

Kết hợp dữ liệu với data.table trong R

Lọc giá trị thiếu

!is.na() có thể dùng để lọc các hàng có giá trị thiếu

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
Kết hợp dữ liệu với data.table trong R

Chỉ giữ kết quả khớp đầu tiên

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

Kết hợp dữ liệu với data.table trong R

Chỉ giữ kết quả khớp cuối cùng

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

Kết hợp dữ liệu với data.table trong R

Nhận diện và loại bỏ trùng lặp

duplicated(): hàng nào bị trùng?

unique(): lọc data.table chỉ còn các hàng duy nhất

Kết hợp dữ liệu với data.table trong R

Hàm duplicated()

Dùng toàn bộ cột:

duplicated(site1_ecology)
FALSE FALSE FALSE FALSE

Dùng một tập con cột:

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

Kết hợp dữ liệu với data.table trong R

Hàm unique()

unique(site1_ecology, by = "genus")

Kết hợp dữ liệu với data.table trong R

Đổi thứ tự tìm kiếm

fromLast = TRUE đổi hướng tìm kiếm, bắt đầu từ hàng cuối

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

Kết hợp dữ liệu với data.table trong R

Cùng luyện tập nào!

Kết hợp dữ liệu với data.table trong R

Preparing Video For Download...