From 0a8220d88260cd69a2e8dd144270a1fb554e4736 Mon Sep 17 00:00:00 2001 From: hadley Date: Tue, 29 Dec 2015 08:22:42 -0600 Subject: [PATCH] Move database transform stuff into own file --- databases.Rmd | 24 ++++++++++++++++++ transform.Rmd | 69 --------------------------------------------------- 2 files changed, 24 insertions(+), 69 deletions(-) create mode 100644 databases.Rmd diff --git a/databases.Rmd b/databases.Rmd new file mode 100644 index 0000000..8011808 --- /dev/null +++ b/databases.Rmd @@ -0,0 +1,24 @@ +--- +layout: default +title: Databases +--- + +### Two-table verbs + +Each two-table verb has a straightforward SQL equivalent: + +| R | SQL +|------------------|-------- +| `inner_join()` | `SELECT * FROM x JOIN y ON x.a = y.a` +| `left_join()` | `SELECT * FROM x LEFT JOIN y ON x.a = y.a` +| `right_join()` | `SELECT * FROM x RIGHT JOIN y ON x.a = y.a` +| `full_join()` | `SELECT * FROM x FULL JOIN y ON x.a = y.a` +| `semi_join()` | `SELECT * FROM x WHERE EXISTS (SELECT 1 FROM y WHERE x.a = y.a)` +| `anti_join()` | `SELECT * FROM x WHERE NOT EXISTS (SELECT 1 FROM y WHERE x.a = y.a)` +| `intersect(x, y)`| `SELECT * FROM x INTERSECT SELECT * FROM y` +| `union(x, y)` | `SELECT * FROM x UNION SELECT * FROM y` +| `setdiff(x, y)` | `SELECT * FROM x EXCEPT SELECT * FROM y` + +`x` and `y` don't have to be tables in the same database. If you specify `copy = TRUE`, dplyr will copy the `y` table into the same location as the `x` variable. This is useful if you've downloaded a summarised dataset and determined a subset of interest that you now want the full data for. You can use `semi_join(x, y, copy = TRUE)` to upload the indices of interest to a temporary table in the same database as `x`, and then perform a efficient semi join in the database. + +If you're working with large data, it maybe also be helpful to set `auto_index = TRUE`. That will automatically add an index on the join variables to the temporary table. diff --git a/transform.Rmd b/transform.Rmd index d2fcaf9..5f4d957 100644 --- a/transform.Rmd +++ b/transform.Rmd @@ -957,72 +957,3 @@ union(df1, df2) setdiff(df1, df2) setdiff(df2, df1) ``` - -### Databases - -Each two-table verb has a straightforward SQL equivalent: - -| R | SQL -|------------------|-------- -| `inner_join()` | `SELECT * FROM x JOIN y ON x.a = y.a` -| `left_join()` | `SELECT * FROM x LEFT JOIN y ON x.a = y.a` -| `right_join()` | `SELECT * FROM x RIGHT JOIN y ON x.a = y.a` -| `full_join()` | `SELECT * FROM x FULL JOIN y ON x.a = y.a` -| `semi_join()` | `SELECT * FROM x WHERE EXISTS (SELECT 1 FROM y WHERE x.a = y.a)` -| `anti_join()` | `SELECT * FROM x WHERE NOT EXISTS (SELECT 1 FROM y WHERE x.a = y.a)` -| `intersect(x, y)`| `SELECT * FROM x INTERSECT SELECT * FROM y` -| `union(x, y)` | `SELECT * FROM x UNION SELECT * FROM y` -| `setdiff(x, y)` | `SELECT * FROM x EXCEPT SELECT * FROM y` - -`x` and `y` don't have to be tables in the same database. If you specify `copy = TRUE`, dplyr will copy the `y` table into the same location as the `x` variable. This is useful if you've downloaded a summarised dataset and determined a subset of interest that you now want the full data for. You can use `semi_join(x, y, copy = TRUE)` to upload the indices of interest to a temporary table in the same database as `x`, and then perform a efficient semi join in the database. - -If you're working with large data, it maybe also be helpful to set `auto_index = TRUE`. That will automatically add an index on the join variables to the temporary table. - -### Coercion rules - -When joining tables, dplyr is a little more conservative than base R about the types of variable that it considers equivalent. This is mostly likely to surprise if you're working factors: - - * Factors with different levels are coerced to character with a warning: - - ```{r} - df1 <- data_frame(x = 1, y = factor("a")) - df2 <- data_frame(x = 2, y = factor("b")) - full_join(df1, df2) %>% str() - ``` - - * Factors with the same levels in a different order are coerced to character - with a warning: - - ```{r} - df1 <- data_frame(x = 1, y = factor("a", levels = c("a", "b"))) - df2 <- data_frame(x = 2, y = factor("b", levels = c("b", "a"))) - full_join(df1, df2) %>% str() - ``` - - * Factors are preserved only if the levels match exactly: - - ```{r} - df1 <- data_frame(x = 1, y = factor("a", levels = c("a", "b"))) - df2 <- data_frame(x = 2, y = factor("b", levels = c("a", "b"))) - full_join(df1, df2) %>% str() - ``` - - * A factor and a character are coerced to character with a warning: - - ```{r} - df1 <- data_frame(x = 1, y = "a") - df2 <- data_frame(x = 2, y = factor("a")) - full_join(df1, df2) %>% str() - ``` - -Otherwise logicals will be silently upcast to integer, and integer to numeric, but coercing to character will raise an error: - -```{r, error = TRUE} -df1 <- data_frame(x = 1, y = 1L) -df2 <- data_frame(x = 2, y = 1.5) -full_join(df1, df2) %>% str() - -df1 <- data_frame(x = 1, y = 1L) -df2 <- data_frame(x = 2, y = "a") -full_join(df1, df2) %>% str() -```