Presentation is loading. Please wait.

Presentation is loading. Please wait.

R: Working with Databases

Similar presentations


Presentation on theme: "R: Working with Databases"— Presentation transcript:

1 R: Working with Databases
Hydroinformatics – Fall 2015

2 Learning Objectives Install and load packages
Use help() features, documentation, other code, and to determine syntax for unfamiliar functions Describe documentation techniques Use RMySQL to interact with existing databases, create new databases, and import data Describe and visualize data using R statistical and graphical functions

3 RMySQL Package https://github.com/rstats-db/rmysql
How does this documentation style compare to the USGS dataRetrieval Package? to the WaterML package? What is most useful? What is the most confusing thing about using a new package? How do you communicate most effectively with new users? Is this important to you if you are not creating and publishing packages? Why or why not?

4 RMySQL Package Note: You need to actually set the password!
dbDriver tells it which type of Database management system you are working with

5 Interacting with Your Database
Try: Listing all available tables in the database Hint 1: Look up functions ls("package:RMySQL") Hint 2: Use help(“function_name”) to get arguments Try: Creating a new table in the LoganRiverODM Database This code was modified from the sql script that we used to create the tables in the LoganRiverODM database.

6 Interacting with Your Database
What does the argument “n=-1” mean? No need to parse the data – the Fetch function puts the queried data directly into an R data.frame format! Note: What is the n=-1? (Look at the help(dbFetch) for more information on the syntax) No need to parse data – it automatically assigns column names

7 Important Notes MySQL tables are read into R as data.frames, but without coercing character or logical data into factors. Similarly while exporting data.frames, factors are exported as character vectors. Integer columns are usually imported as R integer vectors, except for cases such as BIGINT or UNSIGNED INTEGER which are coerced to R's double precision vectors to avoid truncation Time variables are imported/exported as character data, so you need to convert these to your favorite date/time representation. Additional Notes: Factors are a specific data type for categories or levels Integers in MySQL can be set as INT, BIGINT, UNSIGNED INTEGER, etc., but in R, it does not recognize these different data types. It will either recognize INT as integer, or BIGINT, etc., as a double. Need to convert the time variables

8 Creating and Loading Data for WEAP Model
Hints: Look at the SQL scripts that you used to create the ODM and LoganRiverODM databases for the correct syntax for 2 and 3 1. #Set the driver and connection 2. #Create the database dbSendQuery() 3. #Tell the connection which database to use 4. #Set SQL Mode dbSendQuery(con,"SET SESSION sql_mode = `NO_AUTO_VALUE_ON_ZERO`;") 5. #Load the data downloaded from SNOTEL (This should be a few lines of code from your most recent assignment) 6. #Import the data into tables

9 Visualize and Describe your Data
tml Share your results on the class Google Doc (on canvas)


Download ppt "R: Working with Databases"

Similar presentations


Ads by Google