首页 > 解决方案 > 使用数据表左连接

问题描述

我想知道data.table中以下表达式的等价物是什么:

require(nycflights13)
require(data.table)
require(dplyr)

flights_tv <- flights %>% select(year:day, hour, origin, dest, carrier)

left_join_tv <- flights_tv %>% left_join(airports, c("dest" = "faa"))

标签: rdata.table

解决方案


类似的选择data.table

library(data.table)
out <- as.data.table(airports)[as.data.table(flights)[, .SD,
  .SDcols = c("year", "month", "day", "hour", "origin", "dest", "carrier")],
           on = .(faa = dest)]
all.equal(dim(left_join_tv), dim(out))
#[1] TRUE

推荐阅读