Kết hợp dữ liệu với data.table trong R
Scott Ritchie
Postdoctoral Researcher in Systems Genomics
Điều gì xảy ra khi bạn không dùng đúng cột làm khóa nối?
data.table sai cấu trúcDùng cột khóa có kiểu dữ liệu khác nhau sẽ gây lỗi
customers[web_visits, on = .(age = name)]
Error in bmerge(i, x, leftcols, rightcols, io, xo, roll, rollends,
nomatch, :
typeof x.age (double) != typeof i.name (character)

customers[web_visits, on = .(id)]
Error in bmerge(i, x, leftcols, rightcols, io, xo, roll, rollends,
nomatch, :
typeof x.id (integer) != typeof i.id(character)

merge(customers, web_visits, by.x = "address", by.y = "name", all = TRUE)

customers[web_visits, on = .(address = name)]

customers[web_visits, on = .(address = name), nomatch = 0]

customers[web_visits, on = .(age = duration), nomatch = O]

Tìm hiểu ý nghĩa từng cột trước khi join sẽ giúp bạn tránh lỗi

merge(customers, web_visits, by.x = "name", by.y = "person")customers[web_visits, on = .(name = person)] customers[web_visits, on = c("name" = "person")] key <- c("name" = "person") customers[web_visits, on = key]


merge(purchases, web_visits, by = c("name", "date"))
merge(purchases, web_visits,
by.x = c("name", "date"),
by.y = c("person", "date")
purchases[web_visits, on = .(name, date)]
purchases[web_visits, on = c("name", "date")]
purchases[web_visits, on = .(name = person, date)]
purchases[web_visits, on = c("name" = "person", "date")]
Kết hợp dữ liệu với data.table trong R