Getting into access databases is easy from R. Try out the sample code below using this database.
# database.R
# example code for importing access (and other ODBC/DBMS) databases
### Step 1: get the RODBC package set up
install.packages("RODBC") # run once
library(RODBC)
### Step 2: establish a database connection
# option a: manual
setwd("D:/LearnR") # point to where your database is
mydatabase <-odbcConnectAccess2007("mydatabase.accdb")
# option b: select using a menu instead
mydatabase <- odbcDriverConnect()
### Once you have a connection, you can pull tables or submit queries
# Grab a table as a data.frame by name
nutrition <- sqlFetch(mydatabase,"Nutrition")
# Submit sql-like query to database to get more complex tables or vectors
demographics <- sqlQuery(mydatabase,"select * from Demographics")
demographics
jobs <- sqlQuery(mydatabase,"select Job from Demographics")
jobs
# Write a dataframe back as another table
sqlSave(mydatabase, jobs, tablename = "Job Copy", append = TRUE)
close(mydatabase)
University Operator: (919) 962-2211 | © 2026 The University of North Carolina at Chapel Hill |