Att sammanfoga data med data.table i R
Scott Ritchie
Postdoctoral Researcher in Systems Genomics
Vad händer när du inte använder rätt kolumner som joinnycklar?
data.tableAtt använda joinnycklar med olika datatyper ger ett fel
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]

Lär dig vad varje kolumn representerar innan du joinnar – det hjälper dig att undvika fel

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")]
Att sammanfoga data med data.table i R