I have loaded the following settings and dplyr (0.3) and data.table (1.9.3).
R version 3.1.1 (2014-07-10)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] data.table_1.9.3 dplyr_0.3
loaded via a namespace (and not attached):
[1] assertthat_0.1 DBI_0.3.1 magrittr_1.0.1 parallel_3.1.1 plyr_1.8.1 Rcpp_0.11.2
[7] reshape2_1.4 stringr_0.6.2 tools_3.1.1
Here is the dataset. 2 data.tables and 2 data.frames. These two sets have the same content.
DT_1 = data.table(x = rep(c("a","b","c"), each = 3), y = c(1,3,6), v = 1:9)
DT_2 = data.table(V1 = c("b","c"),foo = c(4,2))
DT_1_df = data.frame(x = rep(c("a","b","c"), each = 3), y = c(1,3,6), v = 1:9)
DT_2_df = data.frame(V1 = c("b","c"),foo = c(4,2))
data.table way
When an inner join is performed on two data tables using the data.table method, we get the following result:
setkey(DT_1, x); setkey(DT_2, V1)
DT_1[DT_2]
x y v foo
1: b 1 4 4
2: b 3 5 4
3: b 6 6 4
4: c 1 7 2
5: c 3 8 2
6: c 6 9 2
dplyr0.3 inner_join on data.tables
It gives an error when using inner_join dplyr on two data tables:
inner_join(DT_1, DT_2, by=("x"="V1"))
Error in setkeyv(x, by$x) : some columns are not in the data.table: V1
dplyr0.3 inner_join on data.frame and data.table
Another error if working with data with a data framework:
inner_join(DT_1, DT_2_df, by = c("x" = "V1"))
Error: Data table joins must be on same key
dplyr0.3 inner_join on data.frames
inner_join, however, works great on dataframes:
inner_join(DT_1_df, DT_2_df, by = c("x" = "V1"))
x y v foo
1 b 1 4 4
2 b 3 5 4
3 b 6 6 4
4 c 1 7 2
5 c 3 8 2
6 c 6 9 2
Can anyone explain why this is happening?