Я хотел бы обновить таблицу в базе данных в r:
library(tidyverse)
library(dbplyr)
library(DBI)
library(glue)
# create a table with empty field to be updated later
# id field is now 'id'
my_mtcars <- mtcars %>%
rownames_to_column() %>%
rename(id = rowname) %>%
mutate(newfield = NA)
# create a temp db
mtcars_db <- tbl_memdb(my_mtcars)
con <- src_memdb()$con
# query
my_query <- glue_sql("select * from my_mtcars", con = con)
my_initial_data <- dbGetQuery(con, my_query)
Переменная my_initial_data теперь является фреймом данных, который я получил от memdb. Это выглядит так:
my_initial_data %>% glimpse
Observations: 32
Variables: 13
$ id <chr> "Mazda RX4", "Mazda RX4 Wag", "Datsun 710", "Hornet 4 Drive", "Hornet Sportabout", "Valiant", "Duster 360", "Merc 24…
$ mpg <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8, 16.4, 17.3, 15.2, 10.4, 10.4, 14.7, 32.4, 30.4, 33…
$ cyl <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8, 8, 8, 8, 4, 4, 4, 8, 6, 8, 4
$ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140.8, 167.6, 167.6, 275.8, 275.8, 275.8, 472.0, 460.0, 440.…
$ hp <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180, 205, 215, 230, 66, 52, 65, 97, 150, 150, 245, 175…
$ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92, 3.07, 3.07, 3.07, 2.93, 3.00, 3.23, 4.08, 4.93, 4.…
$ wt <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150, 3.440, 3.440, 4.070, 3.730, 3.780, 5.250, 5.424, 5.34…
$ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22.90, 18.30, 18.90, 17.40, 17.60, 18.00, 17.98, 17.82, 17.4…
$ vs <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1
$ am <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1
$ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3, 3, 3, 3, 4, 5, 5, 5, 5, 5, 4
$ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2, 2, 4, 2, 1, 2, 2, 4, 6, 8, 2
$ newfield <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
Я хотел бы обновить поле newfield, указав значения в локальном фрейме данных. Это возможно? например,
# would like to do something like this:
# (note how I'm joining as local df with one in the db)
my_new_mtcars <- my_mtcars %>% mutate(newfield = 1:nrow(.)) # now newfield is a sequence of numbers
my_update_query <- glue_sql("update my_mtcars
join my_new_mtcars on my_new_mtcars.id = my_mtcars.id
set my_mtcars.newfield = my_new_mtcars.newfield")
Запрос выглядит хорошо, я думаю?
my_update_query
<SQL> update my_mtcars
join my_new_mtcars on my_new_mtcars.id = my_mtcars.id
set my_mtcars.newfield = my_new_mtcars.newfield
Однако, когда я пытаюсь получить сообщение об ошибке:
dbExecute(conn = con, statement = my_update_query)
Error: near "join": syntax error