Tidy data, wrangling, and pipelines in R

Slides:



Advertisements
Similar presentations
Chapter 4 Joining Multiple Tables
Advertisements

Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Concepts of Database Management Sixth Edition
1 Creating and Tweaking Data HRP223 – 2010 October 24, 2011 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
Linux+ Guide to Linux Certification, Second Edition
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
The Relational Database Model. 2 Objectives How relational database model takes a logical view of data Understand how the relational model’s basic components.
Excel 2007 Part (2) Dr. Susan Al Naqshbandi
Advanced File Processing
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
1 Lab 2 and Merging Data (with SQL) HRP223 – 2009 October 19, 2009 Copyright © Leland Stanford Junior University. All rights reserved. Warning:
Overview Excel is a spreadsheet, a grid made from columns and rows. It is a software program that can make number manipulation easy and somewhat painless.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
DTC Quantitative Methods Summary of some SPSS commands Weeks 1 & 2, January 2012.
Linux+ Guide to Linux Certification, Second Edition
1 PEER Session 02/04/15. 2  Multiple good data management software options exist – quantitative (e.g., SPSS), qualitative (e.g, atlas.ti), mixed (e.g.,
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Creating a data set From paper surveys to excel. STEPS 1.Order your filled questionnaires 2.Number your questionnaires 3.Name your variables. 4.Create.
Chapter 6: Modifying and Combining Data Sets  The SET statement is a powerful statement in the DATA step DATA newdatasetname; SET olddatasetname;.. run;
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Aggregator Stage : Definition : Aggregator classifies data rows from a single input link into groups and calculates totals or other aggregate functions.
Session 15 Merging Data in SPSS
Lesson 5-Exploring Utilities
More SQL: Complex Queries,
Overview of R and ggplot2 for graphics
The Relational Database Model
Chapter 6: Modifying and Combining Data Sets
Containers and Lists CIS 40 – Introduction to Programming in Python
ggplot2 Merrill Rudd TAs: Brooke Davis and Megsie Siple
Database Systems: Design, Implementation, and Management Tenth Edition
Data Cleansing with SQL and R Kevin Feasel
Chapter 4 Relational Model Characteristics
Next Generation R tidyr, dplyr, ggplot2
Lecture 2 The Relational Model
Best practices in R scripting
Data manipulation in R: dplyr
Dplyr I EPID 799C Mon Sep
ECONOMETRICS ii – spring 2018
Chapter 3 The Relational Database Model
© 2011 Pearson Education, Inc. Publishing as Prentice Hall
ETL – Using R Kiran Math Developer : Flour in Greenville SC
Numerical Descriptives in R
regex (Regular Expressions) Examples Logic in Python (and pandas)
Guide To UNIX Using Linux Third Edition
376a. Database Design Dept. of Computer Science Vassar College
Chapter 4 Summary Query.
R Programming For Sql Developers ETL USING R
Tidy data, wrangling, and pipelines in R
CSCI N317 Computation for Scientific Applications Unit R
The Relational Database Model
Lab 2 and Merging Data (with SQL)
Lab 2 HRP223 – 2010 October 18, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected.
Contents Preface I Introduction Lesson Objectives I-2
Chapter 2: Intro to Relational Model
Chapter 8 Advanced SQL.
Overview of R and ggplot2 for graphics
Chapter 2: Intro to Relational Model
Have you signed up (or had) your meeting?
Key Concepts R for Data Science.
Manipulating Data Lesson 3.
R for Epi Workshop Module 2: Data Manipulation & Summary Statistics
Database Systems: Design, Implementation, and Management
regex (Regular Expressions) Examples Logic in Python (and pandas)
Spark with R Martijn Tennekes
Presentation transcript:

Tidy data, wrangling, and pipelines in R R Bootcamp 2017 Michael Hallquist

Data structure semantics Most data wrangling can be accomplished using data.frame objects (or tbl in dplyr). These objects consist of rows and columns. Columns are typically labeled (and represent variables) Rows can be labeled, although often not necessary Datasets contain values: numbers, strings, etc. A value belongs to a variable and an observation. A variable contains all values measuring an attribute (e.g., neuroticism) across units (e.g., people) An observation contains all values measured on the same unit across attributes. Wickham 2016 tidyr tutorial

Data structure semantics http://garrettgman.github.io/tidying/

Data wrangling grammar To communicate effectively about data structure, manipulation, and processing, we need a common set of data wrangling verbs. The data import and transformation cheatsheets (https://www.rstudio.com/resources/cheatsheets/) provide a succinct overview of the tools we will use in bootcamp. R provides a number of other tools for data management, but does not have a unifying conceptual framework. For clarity in your training, we’ll stick closely to functions in the tidyr and dplyr packages developed by Hadley Wickham From Wickham 2014, 2016

Tidy data Each variable forms a column Each observation forms a row Each type of observational unit (e.g., persons, schools, counties) forms a table. Variables that are part of the design (participant number, experimental condition, county ID, etc.), or that may be key categorical moderators, should typically be placed first (to the left), and measured variables thereafter. Wickham 2014

Mess #1: Column headers are values, not var. names Messy Tidy

Data tidying verbs: Gather Gather: combined multiple columns into a single column with a key- value pair format From Boehmke 2015

Mess #2: Multiple variables stored in one column Messy Tidy Data tidying verbs: Separate Separate: split a single variable into multiple variables. Useful when values represent many attributes (e.g., sex and age).

Mess #3: Variables stored in both rows and columns Data tidying verbs: Spread Spread: divide key-value rows into columns Messy Molten Tidy Gather Spread

Summary of data tidying verbs gather: combine multiple columns into a single column with a key- value pair format spread: divide key-value rows into columns unite: merge two columns (variables) into one (pasting together) separate: split a single variable into multiple variables. Useful when values represent many attributes (e.g., sex and age).

Core data wrangling verbs filter: subset or remove observations (rows) select: subset or remove a group of columns (variables) mutate (transform): add or modify one or more variables summarize (aggregate): collapse multiple values into a single value (e.g., by summing or taking means) arrange (sort): change the order of observations

Other data wrangling verbs join (Merge): Combine datasets on matching variable(s) group_by: divide dataset according to one or more categorical variables (factors) ungroup: Remove grouping from data operations rename: change the names of one or more variables recode: change the values of a discrete variable (especially factor) slice: subset rows based on numeric order distinct: Keep observations that are non-redundant (cf. unique)

Data pipelines Traditional R uses chained function calls to join together data operations: > arrange(summarize(filter(data, variable == numeric_value), Total = sum(variable)), desc(Total)) This syntax extends from combinations of functions in math: f(g(x)), where the functions are evaluated from inner to outer. Although this is not a terrible syntax, it gets confusing to keep track of "the output of this function is the input to the next one."

Data pipelines An alternative syntax emerged long ago from Unix terminal programming: [mh ~] find . –iname '*.pdf' | grep –v 'figure' | sort –n The idea of "pipes" and "redirection" in shell scripting is that the command can be read from left to right where the pipe | indicates that the output of left command is provided as input to the right command. This syntax makes multi-step chains easier to see and conceptualize.

Data pipelines The magrittr package introduced a pipe-like operator %>% to support data pipelines in R. It plays well with dplyr: > data %>%     filter(variable == "value") %>%     summarize(Total = sum(variable)) %>%     arrange(desc(Total)) Cf. > arrange(summarize(filter(data, variable == numeric_value), Total = sum(variable)), desc(Total))

Use of "this" dataset in dplyr Sometimes it is useful to refer to the current dataset in data wrangling syntax. Dplyr/magrittr tends to hide this from us for convenience, but it's there under the hood. iris %>% filter(Sepal.Length > 7) is the same as iris %>% filter(., Sepal.Length > 7) So, "." refers to the current dataset in dplyr operations. And if you don't specify where the "." falls, it will always be passed as the first argument to the downstream function.

Join types Inner join: retain (only) observations where this is a match in both datasets (left and right) Left join: Keep all rows in left-hand dataset, add values from right- hand dataset where there is a match. Right join: Keep all rows in right-hand dataset, add values from left- hand dataset where there is a match. For non-matching observations on the left, fill in NA Full join: Combine observations from both datasets based on matching key and fill in NA for non-matches.

Special values to watch out for NA: missing na.rm=TRUE available in many functions Also, see na.omit(), na.exclude(), na.fail(), na.pass() NULL: null set Often used when something is undefined Inf: infinite NaN: Not a number. Result of an invalid computation, e.g., log(-1) Warnings(): If R mentions a warning in data wrangling, make sure you've handled it or know its origin.