Move database transform stuff into own file
This commit is contained in:
parent
a27f519da1
commit
0a8220d882
|
@ -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.
|
|
@ -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()
|
||||
```
|
||||
|
|
Loading…
Reference in New Issue