MENU

Fun & Interesting

Create & Connect to SQL Database in R (Example) | Access, Interface Management & Software Packages

Statistics Globe 13,887 3 years ago
Video Not Working? Fix It Now

How to construct & connect to a SQL database in the R programming language. More details: https://statisticsglobe.com/create-connect-database-r How to run a SQL query in R: https://statisticsglobe.com/run-sql-query-r Profile of Kirby White: https://statisticsglobe.com/kirby-white R code of this video: # install.packages("odbc") # install.packages("DBI") library(odbc) #Contains drivers to connect to a database library(DBI) #Contains functions for interacting with the database con <- DBI::dbConnect(drv = odbc::odbc(), Driver = "driver_name", Server = "server_url", Database = "database_name", user = "user", #optional password = "password") #optional # install.packages("RSQL") # install.packages("RSQLite") # install.packages("tidyverse") library(RSQL) #Generate and Process 'SQL' Queries in R library(RSQLite) #Can create an in-memory SQL database library(tidyverse) #Provides helpful functions and sample data to use in R data("population") population data("who") who #Build the placeholder con <- dbConnect(drv = RSQLite::SQLite(), dbname = ":memory:") dbListTables(con) #Load the population table dbWriteTable(conn = con, name = "population", value = population) #Load the who table dbWriteTable(conn = con, name = "who", value = who) dbListTables(con) res <- DBI::dbGetQuery(conn = con, statement = " SELECT who.country, who.year, who.new_sp_m014, population.population FROM population, who WHERE who.country = 'Afghanistan' AND who.year > 1995 AND who.country = population.country AND who.year = population.year ") res Follow me on Social Media: Facebook – Statistics Globe Page: https://www.facebook.com/statisticsglobecom/ Facebook – Group for Discussions & Questions: https://www.facebook.com/groups/statisticsglobe LinkedIn – Statistics Globe Page: https://www.linkedin.com/company/statisticsglobe/ LinkedIn – Group for Discussions & Questions: https://www.linkedin.com/groups/12555223/ Twitter: https://twitter.com/JoachimSchork Music by bensound.com

Comment