SlideShare a Scribd company logo
1 of 28
Cookies
RBasics
Sept. 28, 2017
10:00-11:30 a.m.
Morgan Library, Computer Classroom 175
DataCleaningUsingR
Nov. 2, 2017
10:00-11:30 a.m.
Morgan Library, Computer Classroom 175
DataWranglingUsingR
Nov. 30, 2017
10:00-11:30 a.m.
Morgan Library, Computer Classroom 175
DataVisualizationUsingR
Feb. 15, 2018
10:00-11:30 a.m.
Morgan Library, Computer Classroom 175
VersionControl UsingGit
March 15, 2018
10:00-11:30 a.m.
Morgan Library, Computer Classroom 175
CreatingReproducibleReports
WithRMarkdown
April 19, 2018
10:00-11:30 a.m.
Morgan Library, Computer Classroom 175
REGISTER ONLINE:
ookies
5
5
5
5
5
5
STER ONLINE:
Basic Data
Analysis using R
C. Tobin Magle, PhD
09-28-2017
10:00-11:30 a.m.
Morgan Library
Computer Classroom 175
Based on http://www.datacarpentry.org/R-ecology-lesson/
Outline
• Intro to R and R studio
• Operators and functions
• Data Frames
• Factors
What is R? R Studio?
• R – a programming language
+ software that interprets it
• RStudio – popular software
to write R scripts and interact
with the R software
• http://www.datacarpentry.org/
R-ecology-
lesson/#setup_instructions
Why learn R
• Research Reproducibility
• Widely used, 10000+ “packages”
• Works on many data types
• Produced high-quality graphics
• Free, open source, cross platform
Setup a working directory
• Start RStudio
• File > New project > New directory > Empty project
• Enter a name for this new folder and choose a convenient
location for it (working directory)
• Click on “Create project”
• Create a data folder in your working directory
• Create a new R script (File > New File > R script) and save it
in your working directory
R Studio Interface
Script vs console
• Both accept commands
• Console: runs the commands
• Doesn’t save*
• Script: commands you want to save for later;
• These commands need to be sent to the console to be run
• Ctrl-enter to send from script to console
Operators
• Symbols that tells R to perform a mathematical or logical
operations
https://www.tutorialspoint.com/r/r_operators.htm
Type Symbol
Arithmetic + - * / ^
Assignment <-
Extraction [ ]
Relational > < == != >= <=
Logical & | !
Assignment operator
• Saves values into variables
• variable <- value
• weight_kg <- 55
• Short key alt- dash
Arithmetic operators
• Does math
• 2+2
• 4*4
• 5/2
• 3-1
• Can be combined with the
assignment operator
• weight_lb <- 2.2*weight_kg
Exercise 1: Operators
Functions and arguments
• A sequence of instructions that perform a task
• Predefined, packages, “home-made”
• Have names
• Accepts arguments (input)
• Return a value (output)
• Examples: sqrt, round
• args(round)
(Down)loading data
• Can download using download.file
• download.file("https://ndownloader.figshare.com/files/2292169",
"data/portal_data_joined.csv")
• Read data using read.csv function
• surveys <- read.csv('data/portal_data_joined.csv')
Storing data in a data frame
1. Rows = observations
2. Cols = variables
3. All values in a column must be the same data type
• (number or text)
4. Data must be “rectangular”
• Same # rows/cols
Inspecting data frames
• head(surveys) = look at first 6 rows (all columns)
• str(surveys) = structure # rows, cols, data types
• nrow(surveys) = number of columns
• ncol(surveys) = number of columns
• names(surveys) = column names
• summary(surveys) = does summary stats for each column
Exercise 2: inspecting data frames
Subsetting
• Use the extraction operator ([ ])
• Row column format: surveys[row,column]
• surveys[1,2] #first row, second column
• Select entire row/col: surveys[,column]
• surveys[1,] #first row, all column
• surveys[,1] #first column, all rows
• Ranges: surveys[a:b, column]
• surveys[1:3, 7] #rows 1-3, 7th column
By column name
• surveys["species_id"] # Result is a data.frame
• surveys[, "species_id"] # Result is a vector
• surveys[["species_id"]] # Result is a vector
• surveys$species_id # Result is a vector
Exercise 3:
1. Create a data frame (surveys_200) containing only the
observations from rows 1 to 200 of the surveys dataset.
2. Use nrow() to subset the last row in surveys_200.
3. Use nrow() to extract the row that is in the middle
surveys_200. Store in a variable called surveys_mid
Factors
•Represent categorical data
•Critical for stats and plotting
•Stored as integers with text labels (levels)
•Can be ordered or unordered
•Orders labels by alpha order of text labels
Functions for factors
• Create: sex <- factor(c("male", "female", "female", "male"))
• Unique text labels: levels(sex)
• Number of levels: nlevels(sex)
• Specify level order: sex <- factor(sex, levels = c("male", "female"))
Converting factors
• To character: as.character(sex)
• To number:
• f <- factor(c(1990, 1983, 1977, 1998, 1990))
• as.numeric(f) # wrong! and there is no warning...
as.numeric(as.character(f)) # works...
• as.numeric(levels(f))[f] # The recommended way.
Example: plotting factors
plot(survey$sex)
Renaming levels
• Label missing values
• sex <- surveys$sex # subset the column
• head(sex) # look at first 6 records
• levels(sex) # look at the factor levels
• levels(sex)[1] <- "missing" # change the first label to “missing”
• levels(sex) # look at factor levels again
• head(sex) # see where missing values were
Exercise 4: Renaming factors
1. Rename “F” and “M” to “female” and “male” respectively.
2. Now that we have renamed the factor level to “missing”, can
you recreate the barplot such that “missing” is last (after
“male”)?
What if you don’t want to use factors?
• Argument: stringsAsFactors=FALSE
## Compare the difference between when the data are being read as
## `factor`, and when they are being read as `character`.
surveys <- read.csv("data/portal_data_joined.csv", stringsAsFactors = TRUE)
str(surveys)
surveys <- read.csv("data/portal_data_joined.csv", stringsAsFactors = FALSE)
str(surveys)
## Convert the column "plot_type" into a factor
surveys$plot_type <- factor(surveys$plot_type)
Saving Data as .csv
• Save a subset of your data
• Name: write.csv
• Input: data frame, destination file, separator
• Output: a file to the specified location
• write.table(surveys, "data/surveys4.tsv", sep = "t")
Need help?
• Email: tobin.magle@colostate.edu
• Data Management Services website:
http://lib.colostate.edu/services/data-management
• Data Carpentry: http://www.datacarpentry.org/
• R Ecology Lesson:
http://www.datacarpentry.org/OpenRefine-ecology-lesson/
• Base R Cheat sheet: https://www.rstudio.com/wp-
content/uploads/2016/10/r-cheat-sheet-3.pdf

More Related Content

What's hot

Intro to Reproducible Research
Intro to Reproducible ResearchIntro to Reproducible Research
Intro to Reproducible ResearchC. Tobin Magle
 
Data Archiving and Sharing
Data Archiving and SharingData Archiving and Sharing
Data Archiving and SharingC. Tobin Magle
 
Datat and donuts: how to write a data management plan
Datat and donuts: how to write a data management planDatat and donuts: how to write a data management plan
Datat and donuts: how to write a data management planC. Tobin Magle
 
Tutorial for Circular and Rectangular Manhattan plots
Tutorial for Circular and Rectangular Manhattan plotsTutorial for Circular and Rectangular Manhattan plots
Tutorial for Circular and Rectangular Manhattan plotsAvjinder (Avi) Kaler
 
useR! 2012 Talk
useR! 2012 TalkuseR! 2012 Talk
useR! 2012 Talkrtelmore
 
Data and Donuts: Data organization
Data and Donuts: Data organizationData and Donuts: Data organization
Data and Donuts: Data organizationC. Tobin Magle
 
Reproducible research: practice
Reproducible research: practiceReproducible research: practice
Reproducible research: practiceC. Tobin Magle
 
Presentation on data preparation with pandas
Presentation on data preparation with pandasPresentation on data preparation with pandas
Presentation on data preparation with pandasAkshitaKanther
 
Why R? A Brief Introduction to the Open Source Statistics Platform
Why R? A Brief Introduction to the Open Source Statistics PlatformWhy R? A Brief Introduction to the Open Source Statistics Platform
Why R? A Brief Introduction to the Open Source Statistics PlatformSyracuse University
 
Training in Analytics, R and Social Media Analytics
Training in Analytics, R and Social Media AnalyticsTraining in Analytics, R and Social Media Analytics
Training in Analytics, R and Social Media AnalyticsAjay Ohri
 
Converting Metadata to Linked Data
Converting Metadata to Linked DataConverting Metadata to Linked Data
Converting Metadata to Linked DataKaren Estlund
 
Python programming
Python programmingPython programming
Python programmingsirikeshava
 
R programming Language , Rahul Singh
R programming Language , Rahul SinghR programming Language , Rahul Singh
R programming Language , Rahul SinghRavi Basil
 
Data Management for librarians
Data Management for librariansData Management for librarians
Data Management for librariansC. Tobin Magle
 

What's hot (20)

Intro to Reproducible Research
Intro to Reproducible ResearchIntro to Reproducible Research
Intro to Reproducible Research
 
Data Archiving and Sharing
Data Archiving and SharingData Archiving and Sharing
Data Archiving and Sharing
 
R program
R programR program
R program
 
Datat and donuts: how to write a data management plan
Datat and donuts: how to write a data management planDatat and donuts: how to write a data management plan
Datat and donuts: how to write a data management plan
 
Tutorial for Circular and Rectangular Manhattan plots
Tutorial for Circular and Rectangular Manhattan plotsTutorial for Circular and Rectangular Manhattan plots
Tutorial for Circular and Rectangular Manhattan plots
 
useR! 2012 Talk
useR! 2012 TalkuseR! 2012 Talk
useR! 2012 Talk
 
Data and Donuts: Data organization
Data and Donuts: Data organizationData and Donuts: Data organization
Data and Donuts: Data organization
 
Reproducible research: practice
Reproducible research: practiceReproducible research: practice
Reproducible research: practice
 
Presentation on data preparation with pandas
Presentation on data preparation with pandasPresentation on data preparation with pandas
Presentation on data preparation with pandas
 
Unit 3
Unit 3Unit 3
Unit 3
 
R language
R languageR language
R language
 
Getting Started with R
Getting Started with RGetting Started with R
Getting Started with R
 
Why R? A Brief Introduction to the Open Source Statistics Platform
Why R? A Brief Introduction to the Open Source Statistics PlatformWhy R? A Brief Introduction to the Open Source Statistics Platform
Why R? A Brief Introduction to the Open Source Statistics Platform
 
Pa2 session 1
Pa2 session 1Pa2 session 1
Pa2 session 1
 
Training in Analytics, R and Social Media Analytics
Training in Analytics, R and Social Media AnalyticsTraining in Analytics, R and Social Media Analytics
Training in Analytics, R and Social Media Analytics
 
Data Analysis With Pandas
Data Analysis With PandasData Analysis With Pandas
Data Analysis With Pandas
 
Converting Metadata to Linked Data
Converting Metadata to Linked DataConverting Metadata to Linked Data
Converting Metadata to Linked Data
 
Python programming
Python programmingPython programming
Python programming
 
R programming Language , Rahul Singh
R programming Language , Rahul SinghR programming Language , Rahul Singh
R programming Language , Rahul Singh
 
Data Management for librarians
Data Management for librariansData Management for librarians
Data Management for librarians
 

Similar to Coding and Cookies: R basics

PostgreSQL - It's kind've a nifty database
PostgreSQL - It's kind've a nifty databasePostgreSQL - It's kind've a nifty database
PostgreSQL - It's kind've a nifty databaseBarry Jones
 
Algorithmic techniques-for-big-data-analysis
Algorithmic techniques-for-big-data-analysisAlgorithmic techniques-for-big-data-analysis
Algorithmic techniques-for-big-data-analysisAtner Yegorov
 
Algorithmic techniques-for-big-data-analysis
Algorithmic techniques-for-big-data-analysisAlgorithmic techniques-for-big-data-analysis
Algorithmic techniques-for-big-data-analysisHiye Biniam
 
Relational Database Design Bootcamp
Relational Database Design BootcampRelational Database Design Bootcamp
Relational Database Design BootcampMark Niebergall
 
Happy Go Programming
Happy Go ProgrammingHappy Go Programming
Happy Go ProgrammingLin Yo-An
 
JIP Pipeline System Introduction
JIP Pipeline System IntroductionJIP Pipeline System Introduction
JIP Pipeline System Introductionthasso23
 
Introduction to R for data science
Introduction to R for data scienceIntroduction to R for data science
Introduction to R for data scienceLong Nguyen
 
AgensGraph Presentation at PGConf.us 2017
AgensGraph Presentation at PGConf.us 2017AgensGraph Presentation at PGConf.us 2017
AgensGraph Presentation at PGConf.us 2017Kisung Kim
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basicsLuigi De Russis
 
04-Data-Analysis-Overview.pptx
04-Data-Analysis-Overview.pptx04-Data-Analysis-Overview.pptx
04-Data-Analysis-Overview.pptxShree Shree
 
Meetup Junio Data Analysis with python 2018
Meetup Junio Data Analysis with python 2018Meetup Junio Data Analysis with python 2018
Meetup Junio Data Analysis with python 2018DataLab Community
 
Table Retrieval and Generation
Table Retrieval and GenerationTable Retrieval and Generation
Table Retrieval and Generationkrisztianbalog
 
Programming in python Unit-1 Part-1
Programming in python Unit-1 Part-1Programming in python Unit-1 Part-1
Programming in python Unit-1 Part-1Vikram Nandini
 
Oleksii Levzhynskyi "Solving behavioral complexity with FRP"
Oleksii Levzhynskyi "Solving behavioral complexity with FRP"Oleksii Levzhynskyi "Solving behavioral complexity with FRP"
Oleksii Levzhynskyi "Solving behavioral complexity with FRP"Fwdays
 
Electronic Grading of Paper Assessments
Electronic Grading of Paper AssessmentsElectronic Grading of Paper Assessments
Electronic Grading of Paper AssessmentsMatthew Leingang
 
Python presentation
Python presentationPython presentation
Python presentationJulia437584
 
managing big data
managing big datamanaging big data
managing big dataSuveeksha
 

Similar to Coding and Cookies: R basics (20)

PostgreSQL - It's kind've a nifty database
PostgreSQL - It's kind've a nifty databasePostgreSQL - It's kind've a nifty database
PostgreSQL - It's kind've a nifty database
 
Algorithmic techniques-for-big-data-analysis
Algorithmic techniques-for-big-data-analysisAlgorithmic techniques-for-big-data-analysis
Algorithmic techniques-for-big-data-analysis
 
Algorithmic techniques-for-big-data-analysis
Algorithmic techniques-for-big-data-analysisAlgorithmic techniques-for-big-data-analysis
Algorithmic techniques-for-big-data-analysis
 
Relational Database Design Bootcamp
Relational Database Design BootcampRelational Database Design Bootcamp
Relational Database Design Bootcamp
 
Pa1 session 5
Pa1 session 5Pa1 session 5
Pa1 session 5
 
Happy Go Programming
Happy Go ProgrammingHappy Go Programming
Happy Go Programming
 
JIP Pipeline System Introduction
JIP Pipeline System IntroductionJIP Pipeline System Introduction
JIP Pipeline System Introduction
 
Introduction to R for data science
Introduction to R for data scienceIntroduction to R for data science
Introduction to R for data science
 
PostgreSQL
PostgreSQLPostgreSQL
PostgreSQL
 
AgensGraph Presentation at PGConf.us 2017
AgensGraph Presentation at PGConf.us 2017AgensGraph Presentation at PGConf.us 2017
AgensGraph Presentation at PGConf.us 2017
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basics
 
04-Data-Analysis-Overview.pptx
04-Data-Analysis-Overview.pptx04-Data-Analysis-Overview.pptx
04-Data-Analysis-Overview.pptx
 
Meetup Junio Data Analysis with python 2018
Meetup Junio Data Analysis with python 2018Meetup Junio Data Analysis with python 2018
Meetup Junio Data Analysis with python 2018
 
Table Retrieval and Generation
Table Retrieval and GenerationTable Retrieval and Generation
Table Retrieval and Generation
 
Programming in python Unit-1 Part-1
Programming in python Unit-1 Part-1Programming in python Unit-1 Part-1
Programming in python Unit-1 Part-1
 
Oleksii Levzhynskyi "Solving behavioral complexity with FRP"
Oleksii Levzhynskyi "Solving behavioral complexity with FRP"Oleksii Levzhynskyi "Solving behavioral complexity with FRP"
Oleksii Levzhynskyi "Solving behavioral complexity with FRP"
 
Electronic Grading of Paper Assessments
Electronic Grading of Paper AssessmentsElectronic Grading of Paper Assessments
Electronic Grading of Paper Assessments
 
Python presentation
Python presentationPython presentation
Python presentation
 
managing big data
managing big datamanaging big data
managing big data
 
Aggregate.pptx
Aggregate.pptxAggregate.pptx
Aggregate.pptx
 

More from C. Tobin Magle

Responsible conduct of research: Data Management
Responsible conduct of research: Data ManagementResponsible conduct of research: Data Management
Responsible conduct of research: Data ManagementC. Tobin Magle
 
Collaborative Data Management using OSF
Collaborative Data Management using OSFCollaborative Data Management using OSF
Collaborative Data Management using OSFC. Tobin Magle
 
Data Management Services at the Morgan Library
Data Management Services at the Morgan LibraryData Management Services at the Morgan Library
Data Management Services at the Morgan LibraryC. Tobin Magle
 
Data and Donuts: How to write a data management plan
Data and Donuts: How to write a data management planData and Donuts: How to write a data management plan
Data and Donuts: How to write a data management planC. Tobin Magle
 
Data and Donuts: The Impact of Data Management
Data and Donuts: The Impact of Data ManagementData and Donuts: The Impact of Data Management
Data and Donuts: The Impact of Data ManagementC. Tobin Magle
 
Bringing bioinformatics into the library
Bringing bioinformatics into the libraryBringing bioinformatics into the library
Bringing bioinformatics into the libraryC. Tobin Magle
 
Reproducible research: theory
Reproducible research: theoryReproducible research: theory
Reproducible research: theoryC. Tobin Magle
 
CU Anschutz Health Science Library Data Services
CU Anschutz Health Science Library Data ServicesCU Anschutz Health Science Library Data Services
CU Anschutz Health Science Library Data ServicesC. Tobin Magle
 
Magle data curation in libraries
Magle data curation in librariesMagle data curation in libraries
Magle data curation in librariesC. Tobin Magle
 

More from C. Tobin Magle (10)

Responsible conduct of research: Data Management
Responsible conduct of research: Data ManagementResponsible conduct of research: Data Management
Responsible conduct of research: Data Management
 
Collaborative Data Management using OSF
Collaborative Data Management using OSFCollaborative Data Management using OSF
Collaborative Data Management using OSF
 
Data Management Services at the Morgan Library
Data Management Services at the Morgan LibraryData Management Services at the Morgan Library
Data Management Services at the Morgan Library
 
Open access day
Open access dayOpen access day
Open access day
 
Data and Donuts: How to write a data management plan
Data and Donuts: How to write a data management planData and Donuts: How to write a data management plan
Data and Donuts: How to write a data management plan
 
Data and Donuts: The Impact of Data Management
Data and Donuts: The Impact of Data ManagementData and Donuts: The Impact of Data Management
Data and Donuts: The Impact of Data Management
 
Bringing bioinformatics into the library
Bringing bioinformatics into the libraryBringing bioinformatics into the library
Bringing bioinformatics into the library
 
Reproducible research: theory
Reproducible research: theoryReproducible research: theory
Reproducible research: theory
 
CU Anschutz Health Science Library Data Services
CU Anschutz Health Science Library Data ServicesCU Anschutz Health Science Library Data Services
CU Anschutz Health Science Library Data Services
 
Magle data curation in libraries
Magle data curation in librariesMagle data curation in libraries
Magle data curation in libraries
 

Recently uploaded

Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一fhwihughh
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxFurkanTasci3
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一F sss
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...Suhani Kapoor
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样vhwb25kk
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceSapana Sha
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改atducpo
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDRafezzaman
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 

Recently uploaded (20)

Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptx
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
 
Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts Service
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 

Coding and Cookies: R basics

  • 1. Cookies RBasics Sept. 28, 2017 10:00-11:30 a.m. Morgan Library, Computer Classroom 175 DataCleaningUsingR Nov. 2, 2017 10:00-11:30 a.m. Morgan Library, Computer Classroom 175 DataWranglingUsingR Nov. 30, 2017 10:00-11:30 a.m. Morgan Library, Computer Classroom 175 DataVisualizationUsingR Feb. 15, 2018 10:00-11:30 a.m. Morgan Library, Computer Classroom 175 VersionControl UsingGit March 15, 2018 10:00-11:30 a.m. Morgan Library, Computer Classroom 175 CreatingReproducibleReports WithRMarkdown April 19, 2018 10:00-11:30 a.m. Morgan Library, Computer Classroom 175 REGISTER ONLINE: ookies 5 5 5 5 5 5 STER ONLINE: Basic Data Analysis using R C. Tobin Magle, PhD 09-28-2017 10:00-11:30 a.m. Morgan Library Computer Classroom 175 Based on http://www.datacarpentry.org/R-ecology-lesson/
  • 2. Outline • Intro to R and R studio • Operators and functions • Data Frames • Factors
  • 3. What is R? R Studio? • R – a programming language + software that interprets it • RStudio – popular software to write R scripts and interact with the R software • http://www.datacarpentry.org/ R-ecology- lesson/#setup_instructions
  • 4. Why learn R • Research Reproducibility • Widely used, 10000+ “packages” • Works on many data types • Produced high-quality graphics • Free, open source, cross platform
  • 5. Setup a working directory • Start RStudio • File > New project > New directory > Empty project • Enter a name for this new folder and choose a convenient location for it (working directory) • Click on “Create project” • Create a data folder in your working directory • Create a new R script (File > New File > R script) and save it in your working directory
  • 7. Script vs console • Both accept commands • Console: runs the commands • Doesn’t save* • Script: commands you want to save for later; • These commands need to be sent to the console to be run • Ctrl-enter to send from script to console
  • 8. Operators • Symbols that tells R to perform a mathematical or logical operations https://www.tutorialspoint.com/r/r_operators.htm Type Symbol Arithmetic + - * / ^ Assignment <- Extraction [ ] Relational > < == != >= <= Logical & | !
  • 9. Assignment operator • Saves values into variables • variable <- value • weight_kg <- 55 • Short key alt- dash
  • 10. Arithmetic operators • Does math • 2+2 • 4*4 • 5/2 • 3-1 • Can be combined with the assignment operator • weight_lb <- 2.2*weight_kg
  • 12. Functions and arguments • A sequence of instructions that perform a task • Predefined, packages, “home-made” • Have names • Accepts arguments (input) • Return a value (output) • Examples: sqrt, round • args(round)
  • 13. (Down)loading data • Can download using download.file • download.file("https://ndownloader.figshare.com/files/2292169", "data/portal_data_joined.csv") • Read data using read.csv function • surveys <- read.csv('data/portal_data_joined.csv')
  • 14. Storing data in a data frame 1. Rows = observations 2. Cols = variables 3. All values in a column must be the same data type • (number or text) 4. Data must be “rectangular” • Same # rows/cols
  • 15. Inspecting data frames • head(surveys) = look at first 6 rows (all columns) • str(surveys) = structure # rows, cols, data types • nrow(surveys) = number of columns • ncol(surveys) = number of columns • names(surveys) = column names • summary(surveys) = does summary stats for each column
  • 16. Exercise 2: inspecting data frames
  • 17. Subsetting • Use the extraction operator ([ ]) • Row column format: surveys[row,column] • surveys[1,2] #first row, second column • Select entire row/col: surveys[,column] • surveys[1,] #first row, all column • surveys[,1] #first column, all rows • Ranges: surveys[a:b, column] • surveys[1:3, 7] #rows 1-3, 7th column
  • 18. By column name • surveys["species_id"] # Result is a data.frame • surveys[, "species_id"] # Result is a vector • surveys[["species_id"]] # Result is a vector • surveys$species_id # Result is a vector
  • 19. Exercise 3: 1. Create a data frame (surveys_200) containing only the observations from rows 1 to 200 of the surveys dataset. 2. Use nrow() to subset the last row in surveys_200. 3. Use nrow() to extract the row that is in the middle surveys_200. Store in a variable called surveys_mid
  • 20. Factors •Represent categorical data •Critical for stats and plotting •Stored as integers with text labels (levels) •Can be ordered or unordered •Orders labels by alpha order of text labels
  • 21. Functions for factors • Create: sex <- factor(c("male", "female", "female", "male")) • Unique text labels: levels(sex) • Number of levels: nlevels(sex) • Specify level order: sex <- factor(sex, levels = c("male", "female"))
  • 22. Converting factors • To character: as.character(sex) • To number: • f <- factor(c(1990, 1983, 1977, 1998, 1990)) • as.numeric(f) # wrong! and there is no warning... as.numeric(as.character(f)) # works... • as.numeric(levels(f))[f] # The recommended way.
  • 24. Renaming levels • Label missing values • sex <- surveys$sex # subset the column • head(sex) # look at first 6 records • levels(sex) # look at the factor levels • levels(sex)[1] <- "missing" # change the first label to “missing” • levels(sex) # look at factor levels again • head(sex) # see where missing values were
  • 25. Exercise 4: Renaming factors 1. Rename “F” and “M” to “female” and “male” respectively. 2. Now that we have renamed the factor level to “missing”, can you recreate the barplot such that “missing” is last (after “male”)?
  • 26. What if you don’t want to use factors? • Argument: stringsAsFactors=FALSE ## Compare the difference between when the data are being read as ## `factor`, and when they are being read as `character`. surveys <- read.csv("data/portal_data_joined.csv", stringsAsFactors = TRUE) str(surveys) surveys <- read.csv("data/portal_data_joined.csv", stringsAsFactors = FALSE) str(surveys) ## Convert the column "plot_type" into a factor surveys$plot_type <- factor(surveys$plot_type)
  • 27. Saving Data as .csv • Save a subset of your data • Name: write.csv • Input: data frame, destination file, separator • Output: a file to the specified location • write.table(surveys, "data/surveys4.tsv", sep = "t")
  • 28. Need help? • Email: tobin.magle@colostate.edu • Data Management Services website: http://lib.colostate.edu/services/data-management • Data Carpentry: http://www.datacarpentry.org/ • R Ecology Lesson: http://www.datacarpentry.org/OpenRefine-ecology-lesson/ • Base R Cheat sheet: https://www.rstudio.com/wp- content/uploads/2016/10/r-cheat-sheet-3.pdf