SlideShare uma empresa Scribd logo
1 de 27
Baixar para ler offline
R FOR PYTHONISTAS
CHRISTOPHER ROACH
REASONS FOR LEARNING R
It's data-centric. It was literally made for interacting with data.
It has tons of battle-tested, high-quality packages for nearly every type of analysis.
Its data visualization capabilities (ahem, ggplot2) are second to none.
It just seems more natural when dealing with data (functional, dplyr, magrittr).
It's interesting from a language perspective
It can be a good source of ideas for the Python community
MORE RESOURCES FOR LEARNING DATA SCIENCE
THE RSTUDIO SUITE OF TOOLS
Source:
by David Robinson
"What are the Most Disliked Programming Languages?"
(https://stackoverflow.blog/2017/10/31/disliked-programming-languages/)
WHAT YOU NEED TO KNOW...
AS A PYTHON DEVELOPER
DOTS (.) VS DOLLAR SIGNS ($)
Legal characters for variable and function names include:
letters
numbers
underscores (_)
dots (.)
In [1]: str(data.frame)
function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,
fix.empty.names = TRUE, stringsAsFactors = default.stringsAsFactors())
Indexing, or attribute access, is done with the
dollar sign ($)
double brackets ([[)
In [2]:
In [3]:
me <- list(
first_name = 'Christopher',
last_name = 'Roach',
title = 'Software Engineer',
company = 'LinkedIn',
start_date = as.Date('2013-06-01')
)
# Nice (tidyverse) library for string manipulation
library(glue)
# Use the `$` operator/function to access the values in `me`
print(glue("{me$first_name} {me[[2]]}",
"has been a {me$title} at {me$company}",
"since {me$start_date}", .sep = " "))
Christopher Roach has been a Software Engineer at LinkedIn since 2013-06-01
ASSIGNMENT OPERATORS: = VS <- VS <<-
A holdover from the APL language, where = was used for testing equality.
<- and = work the same in modern R (since 2001), however...
In [4]:
In [5]:
str(mean)
# Evaluate the code in a local environment to avoid polluting the global env
local({
# Assign the value of the call to runif to the x arg
mean(x = runif(10))
print(paste("ls() =>", ls()))
})
function (x, ...)
[1] "ls() => "
In [6]: local({
# Assign the value of the call to runif to y,
# and call mean with the first argument set to y
mean(y <- runif(10))
print(paste("ls() =>", ls()))
})
[1] "ls() => y"
The deep assignment operator (<<-) modifies variables in the parent environments
The closest Python analog would be the global keyword
In [7]: by_2 <- (function(offset) {
i <- 0
function() {
i <<- i + offset
}
})(2)
for (i in 1:5) { print(by_2()) }
[1] 2
[1] 4
[1] 6
[1] 8
[1] 10
EVERYTHING IS A VECTOR
As a language whose sole purpose in life is to deal with data, R takes a very different view of the
world from most programming languages. In particular, R views all data as plural. In fact, it is
absolutely impossible to have data in R that is just a singular value. In R’s eyes, everything is a
vector.
In [8]:
In [9]:
In [10]:
In [11]:
numbers <- 42
length(numbers)
typeof(numbers)
42[1]
42[2]
1
'double'
42
<NA>
THIS ALLOWS US TO DO SOME REALLY WEIRD THINGS...
In [12]: v <- c(1,3,5,7)
v[2][1][1][1][1][1][1][1][1][1][1][1]
3
THIS IS WHY THINGS LIKE THIS WORK AUTOMATICALY IN R...
In [13]: v %% 3
1 0 2 1
R IS FUNCTIONAL
First class functions
Higher order functions
Lambdas/Closures
It allows “computing on the language”
Attempts to be pure
In [14]: # Toolset for introspecting the R language
library(pryr)
# Using `local` to prevent a second reference to `x`
local({
x <- list(name = 'Chris')
print(refs(x))
print(address(x))
# Update the variable and
x$name <- 'Christopher'
print(address(x))
# Increase the reference count on l
y <- x
print(refs(x))
# And, now R should switch to copy-on-modify semantics
x$name <- 'Christopher Roach'
print(address(x))
})
[1] 1
[1] "0x7fa2eaf1a848"
[1] "0x7fa2eaf1a848"
[1] 2
[1] "0x7fa2eaf29d18"
OOP IN R
R has several different "systems" for OOP
Two styles of OOP systems:
Encapsulated - methods belong to objects or classes
Functional - methods belong to functions called generics
S3 is a functional system used throughout base R
S4 is a functional system like S3, but more formal
RC is an encapsulated system that bypasses R's copy-on-modify semantics
R6 is an encapsulated system similar to RC, but are lighter weight and avoids some of
RC's issues
THE S3 OOP SYSTEM
S3 is an informal system that relies mainly on convention
Methods are the functions that implement the class-specific behavior
Generic functions, or generics for short, are responsible for selecting the correct
method to apply
Method dispatch is based on an object's class attribute.
In [15]:
In [16]:
In [17]:
# A factor object can only be intergers
sizes <- c(1L, 1L, 2L, 2L, 2L, 3L, 4L)
print(sizes)
# Set"levels" and "class" to change "sizes" into a factor
attr(sizes, "levels") <- c("S", "M", "L", "XL")
attr(sizes, "class") <- "factor"
# Now, print the new factor vector
print(sizes)
str(print.factor)
[1] 1 1 2 2 2 3 4
[1] S S M M M L XL
Levels: S M L XL
function (x, quote = FALSE, max.levels = NULL, width = getOption("width"),
...)
OVERRIDING BEHAVIOR IN S3
In [18]: # Cache the old print.factor function and create a new one
old.print.factor <- print.factor
print.factor <- function(...) {
print("We are printing from the new print.factor function...")
old.print.factor(...)
}
# Print the sizes object, which will call our new print.factor function
print(sizes)
# Then reset the print.factor method back to the original one
print.factor <- old.print.factor
[1] "We are printing from the new print.factor function..."
[1] S S M M M L XL
Levels: S M L XL
CREATING CLASSES IN S3
In [19]: attr(me, 'class') <- 'employee'
print.employee <- function(e) {
print(glue("{e$first_name} {e$last_name}",
"has been a {e$title} at {e$company}",
"since {e$start_date}", .sep = " "))
}
print(me)
Christopher Roach has been a Software Engineer at LinkedIn since 2013-06-01
NON-STANDARD EVALUATION
Function arguments in R are lazily evaluated
In [20]: # `x` and `y` do not exist
mean(runif(100), foo = x + y)
0.457389679029584
This allows us to capture the code for each argument.
And, do fun things with it!
In [21]: print_expr <- function(expr, ...) {
expr <- substitute(expr)
params <- list(...)
val <- eval(expr, params)
print(glue("The value of {deparse(expr)} is {val}"))
}
print_expr(x + y, x = 4, y = 12)
The value of x + y is 16
In [23]: library(ggplot2)
ggplot(mtcars, mapping = aes(x = hp, y = mpg)) +
geom_point()
COMBINING PYTHON AND R
Since this is an R notebook, and I want to demonstrate how you can access the power of R from
Python, we need to switch to another notebook. To see how you can incorporate R into your
normal Python and Jupyter workflow, read over
.
this notebook
(https://github.com/croach/pydata_nyc_2017/blob/master/notebooks/rpy2.ipynb)

Mais conteúdo relacionado

Mais procurados

Poor Man's Functional Programming
Poor Man's Functional ProgrammingPoor Man's Functional Programming
Poor Man's Functional ProgrammingDmitry Buzdin
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)Pedro Rodrigues
 
Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?Lucas Witold Adamus
 
Introduction to R
Introduction to RIntroduction to R
Introduction to Ragnonchik
 
python beginner talk slide
python beginner talk slidepython beginner talk slide
python beginner talk slidejonycse
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Jesper Kamstrup Linnet
 
Python dictionary : past, present, future
Python dictionary: past, present, futurePython dictionary: past, present, future
Python dictionary : past, present, futuredelimitry
 
Postgresql 9.3 overview
Postgresql 9.3 overviewPostgresql 9.3 overview
Postgresql 9.3 overviewAveic
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheetGil Cohen
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meetMario Fusco
 
Python fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanPython fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanWei-Yuan Chang
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)gekiaruj
 
Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!priort
 

Mais procurados (20)

Poor Man's Functional Programming
Poor Man's Functional ProgrammingPoor Man's Functional Programming
Poor Man's Functional Programming
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)
 
Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
 
python beginner talk slide
python beginner talk slidepython beginner talk slide
python beginner talk slide
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 
Language R
Language RLanguage R
Language R
 
C# 7
C# 7C# 7
C# 7
 
Python dictionary : past, present, future
Python dictionary: past, present, futurePython dictionary: past, present, future
Python dictionary : past, present, future
 
Postgresql 9.3 overview
Postgresql 9.3 overviewPostgresql 9.3 overview
Postgresql 9.3 overview
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheet
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
Statistical computing 01
Statistical computing 01Statistical computing 01
Statistical computing 01
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
 
Python fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanPython fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuan
 
Python Cheat Sheet
Python Cheat SheetPython Cheat Sheet
Python Cheat Sheet
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)
 
Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!
 

Semelhante a R for Pythonistas (PyData NYC 2017)

Poetry with R -- Dissecting the code
Poetry with R -- Dissecting the codePoetry with R -- Dissecting the code
Poetry with R -- Dissecting the codePeter Solymos
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programmingNimrita Koul
 
Morel, a data-parallel programming language
Morel, a data-parallel programming languageMorel, a data-parallel programming language
Morel, a data-parallel programming languageJulian Hyde
 
Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programaciónSoftware Guru
 
Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptModeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptanshikagoel52
 
Morel, a Functional Query Language
Morel, a Functional Query LanguageMorel, a Functional Query Language
Morel, a Functional Query LanguageJulian Hyde
 
2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factorskrishna singh
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programmingAlberto Labarga
 
A short tutorial on r
A short tutorial on rA short tutorial on r
A short tutorial on rAshraf Uddin
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Guy Lebanon
 
Python workshop intro_string (1)
Python workshop intro_string (1)Python workshop intro_string (1)
Python workshop intro_string (1)Karamjit Kaur
 
Best corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbaiBest corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbaiUnmesh Baile
 

Semelhante a R for Pythonistas (PyData NYC 2017) (20)

Poetry with R -- Dissecting the code
Poetry with R -- Dissecting the codePoetry with R -- Dissecting the code
Poetry with R -- Dissecting the code
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programming
 
R basics
R basicsR basics
R basics
 
Python basic
Python basicPython basic
Python basic
 
Morel, a data-parallel programming language
Morel, a data-parallel programming languageMorel, a data-parallel programming language
Morel, a data-parallel programming language
 
Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programación
 
R language introduction
R language introductionR language introduction
R language introduction
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
 
Lecture1 r
Lecture1 rLecture1 r
Lecture1 r
 
Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptModeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.ppt
 
Morel, a Functional Query Language
Morel, a Functional Query LanguageMorel, a Functional Query Language
Morel, a Functional Query Language
 
2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programming
 
A short tutorial on r
A short tutorial on rA short tutorial on r
A short tutorial on r
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)
 
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
 
Python workshop intro_string (1)
Python workshop intro_string (1)Python workshop intro_string (1)
Python workshop intro_string (1)
 
Best corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbaiBest corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbai
 

Último

Switzerland Constitution 2002.pdf.........
Switzerland Constitution 2002.pdf.........Switzerland Constitution 2002.pdf.........
Switzerland Constitution 2002.pdf.........EfruzAsilolu
 
7. Epi of Chronic respiratory diseases.ppt
7. Epi of Chronic respiratory diseases.ppt7. Epi of Chronic respiratory diseases.ppt
7. Epi of Chronic respiratory diseases.pptibrahimabdi22
 
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...gajnagarg
 
Dubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls DubaiDubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls Dubaikojalkojal131
 
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...nirzagarg
 
Harnessing the Power of GenAI for BI and Reporting.pptx
Harnessing the Power of GenAI for BI and Reporting.pptxHarnessing the Power of GenAI for BI and Reporting.pptx
Harnessing the Power of GenAI for BI and Reporting.pptxParas Gupta
 
The-boAt-Story-Navigating-the-Waves-of-Innovation.pptx
The-boAt-Story-Navigating-the-Waves-of-Innovation.pptxThe-boAt-Story-Navigating-the-Waves-of-Innovation.pptx
The-boAt-Story-Navigating-the-Waves-of-Innovation.pptxVivek487417
 
Reconciling Conflicting Data Curation Actions: Transparency Through Argument...
Reconciling Conflicting Data Curation Actions:  Transparency Through Argument...Reconciling Conflicting Data Curation Actions:  Transparency Through Argument...
Reconciling Conflicting Data Curation Actions: Transparency Through Argument...Bertram Ludäscher
 
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样wsppdmt
 
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...nirzagarg
 
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling ManjurJual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjurptikerjasaptiker
 
PLE-statistics document for primary schs
PLE-statistics document for primary schsPLE-statistics document for primary schs
PLE-statistics document for primary schscnajjemba
 
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制vexqp
 
SR-101-01012024-EN.docx Federal Constitution of the Swiss Confederation
SR-101-01012024-EN.docx  Federal Constitution  of the Swiss ConfederationSR-101-01012024-EN.docx  Federal Constitution  of the Swiss Confederation
SR-101-01012024-EN.docx Federal Constitution of the Swiss ConfederationEfruzAsilolu
 
Predicting HDB Resale Prices - Conducting Linear Regression Analysis With Orange
Predicting HDB Resale Prices - Conducting Linear Regression Analysis With OrangePredicting HDB Resale Prices - Conducting Linear Regression Analysis With Orange
Predicting HDB Resale Prices - Conducting Linear Regression Analysis With OrangeThinkInnovation
 
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...gajnagarg
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRajesh Mondal
 
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制vexqp
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
Gartner's Data Analytics Maturity Model.pptx
Gartner's Data Analytics Maturity Model.pptxGartner's Data Analytics Maturity Model.pptx
Gartner's Data Analytics Maturity Model.pptxchadhar227
 

Último (20)

Switzerland Constitution 2002.pdf.........
Switzerland Constitution 2002.pdf.........Switzerland Constitution 2002.pdf.........
Switzerland Constitution 2002.pdf.........
 
7. Epi of Chronic respiratory diseases.ppt
7. Epi of Chronic respiratory diseases.ppt7. Epi of Chronic respiratory diseases.ppt
7. Epi of Chronic respiratory diseases.ppt
 
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
 
Dubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls DubaiDubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls Dubai
 
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
 
Harnessing the Power of GenAI for BI and Reporting.pptx
Harnessing the Power of GenAI for BI and Reporting.pptxHarnessing the Power of GenAI for BI and Reporting.pptx
Harnessing the Power of GenAI for BI and Reporting.pptx
 
The-boAt-Story-Navigating-the-Waves-of-Innovation.pptx
The-boAt-Story-Navigating-the-Waves-of-Innovation.pptxThe-boAt-Story-Navigating-the-Waves-of-Innovation.pptx
The-boAt-Story-Navigating-the-Waves-of-Innovation.pptx
 
Reconciling Conflicting Data Curation Actions: Transparency Through Argument...
Reconciling Conflicting Data Curation Actions:  Transparency Through Argument...Reconciling Conflicting Data Curation Actions:  Transparency Through Argument...
Reconciling Conflicting Data Curation Actions: Transparency Through Argument...
 
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
 
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
 
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling ManjurJual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
 
PLE-statistics document for primary schs
PLE-statistics document for primary schsPLE-statistics document for primary schs
PLE-statistics document for primary schs
 
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
 
SR-101-01012024-EN.docx Federal Constitution of the Swiss Confederation
SR-101-01012024-EN.docx  Federal Constitution  of the Swiss ConfederationSR-101-01012024-EN.docx  Federal Constitution  of the Swiss Confederation
SR-101-01012024-EN.docx Federal Constitution of the Swiss Confederation
 
Predicting HDB Resale Prices - Conducting Linear Regression Analysis With Orange
Predicting HDB Resale Prices - Conducting Linear Regression Analysis With OrangePredicting HDB Resale Prices - Conducting Linear Regression Analysis With Orange
Predicting HDB Resale Prices - Conducting Linear Regression Analysis With Orange
 
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for Research
 
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Gartner's Data Analytics Maturity Model.pptx
Gartner's Data Analytics Maturity Model.pptxGartner's Data Analytics Maturity Model.pptx
Gartner's Data Analytics Maturity Model.pptx
 

R for Pythonistas (PyData NYC 2017)

  • 2. REASONS FOR LEARNING R It's data-centric. It was literally made for interacting with data. It has tons of battle-tested, high-quality packages for nearly every type of analysis. Its data visualization capabilities (ahem, ggplot2) are second to none. It just seems more natural when dealing with data (functional, dplyr, magrittr). It's interesting from a language perspective It can be a good source of ideas for the Python community
  • 3. MORE RESOURCES FOR LEARNING DATA SCIENCE
  • 4. THE RSTUDIO SUITE OF TOOLS
  • 5. Source: by David Robinson "What are the Most Disliked Programming Languages?" (https://stackoverflow.blog/2017/10/31/disliked-programming-languages/)
  • 6. WHAT YOU NEED TO KNOW... AS A PYTHON DEVELOPER
  • 7. DOTS (.) VS DOLLAR SIGNS ($)
  • 8. Legal characters for variable and function names include: letters numbers underscores (_) dots (.) In [1]: str(data.frame) function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, fix.empty.names = TRUE, stringsAsFactors = default.stringsAsFactors())
  • 9. Indexing, or attribute access, is done with the dollar sign ($) double brackets ([[) In [2]: In [3]: me <- list( first_name = 'Christopher', last_name = 'Roach', title = 'Software Engineer', company = 'LinkedIn', start_date = as.Date('2013-06-01') ) # Nice (tidyverse) library for string manipulation library(glue) # Use the `$` operator/function to access the values in `me` print(glue("{me$first_name} {me[[2]]}", "has been a {me$title} at {me$company}", "since {me$start_date}", .sep = " ")) Christopher Roach has been a Software Engineer at LinkedIn since 2013-06-01
  • 10. ASSIGNMENT OPERATORS: = VS <- VS <<- A holdover from the APL language, where = was used for testing equality. <- and = work the same in modern R (since 2001), however...
  • 11. In [4]: In [5]: str(mean) # Evaluate the code in a local environment to avoid polluting the global env local({ # Assign the value of the call to runif to the x arg mean(x = runif(10)) print(paste("ls() =>", ls())) }) function (x, ...) [1] "ls() => "
  • 12. In [6]: local({ # Assign the value of the call to runif to y, # and call mean with the first argument set to y mean(y <- runif(10)) print(paste("ls() =>", ls())) }) [1] "ls() => y"
  • 13. The deep assignment operator (<<-) modifies variables in the parent environments The closest Python analog would be the global keyword In [7]: by_2 <- (function(offset) { i <- 0 function() { i <<- i + offset } })(2) for (i in 1:5) { print(by_2()) } [1] 2 [1] 4 [1] 6 [1] 8 [1] 10
  • 14. EVERYTHING IS A VECTOR As a language whose sole purpose in life is to deal with data, R takes a very different view of the world from most programming languages. In particular, R views all data as plural. In fact, it is absolutely impossible to have data in R that is just a singular value. In R’s eyes, everything is a vector. In [8]: In [9]: In [10]: In [11]: numbers <- 42 length(numbers) typeof(numbers) 42[1] 42[2] 1 'double' 42 <NA>
  • 15. THIS ALLOWS US TO DO SOME REALLY WEIRD THINGS... In [12]: v <- c(1,3,5,7) v[2][1][1][1][1][1][1][1][1][1][1][1] 3
  • 16. THIS IS WHY THINGS LIKE THIS WORK AUTOMATICALY IN R... In [13]: v %% 3 1 0 2 1
  • 17. R IS FUNCTIONAL First class functions Higher order functions Lambdas/Closures It allows “computing on the language” Attempts to be pure
  • 18. In [14]: # Toolset for introspecting the R language library(pryr) # Using `local` to prevent a second reference to `x` local({ x <- list(name = 'Chris') print(refs(x)) print(address(x)) # Update the variable and x$name <- 'Christopher' print(address(x)) # Increase the reference count on l y <- x print(refs(x)) # And, now R should switch to copy-on-modify semantics x$name <- 'Christopher Roach' print(address(x)) }) [1] 1 [1] "0x7fa2eaf1a848" [1] "0x7fa2eaf1a848" [1] 2 [1] "0x7fa2eaf29d18"
  • 19. OOP IN R R has several different "systems" for OOP Two styles of OOP systems: Encapsulated - methods belong to objects or classes Functional - methods belong to functions called generics S3 is a functional system used throughout base R S4 is a functional system like S3, but more formal RC is an encapsulated system that bypasses R's copy-on-modify semantics R6 is an encapsulated system similar to RC, but are lighter weight and avoids some of RC's issues
  • 20. THE S3 OOP SYSTEM S3 is an informal system that relies mainly on convention Methods are the functions that implement the class-specific behavior Generic functions, or generics for short, are responsible for selecting the correct method to apply Method dispatch is based on an object's class attribute.
  • 21. In [15]: In [16]: In [17]: # A factor object can only be intergers sizes <- c(1L, 1L, 2L, 2L, 2L, 3L, 4L) print(sizes) # Set"levels" and "class" to change "sizes" into a factor attr(sizes, "levels") <- c("S", "M", "L", "XL") attr(sizes, "class") <- "factor" # Now, print the new factor vector print(sizes) str(print.factor) [1] 1 1 2 2 2 3 4 [1] S S M M M L XL Levels: S M L XL function (x, quote = FALSE, max.levels = NULL, width = getOption("width"), ...)
  • 22. OVERRIDING BEHAVIOR IN S3 In [18]: # Cache the old print.factor function and create a new one old.print.factor <- print.factor print.factor <- function(...) { print("We are printing from the new print.factor function...") old.print.factor(...) } # Print the sizes object, which will call our new print.factor function print(sizes) # Then reset the print.factor method back to the original one print.factor <- old.print.factor [1] "We are printing from the new print.factor function..." [1] S S M M M L XL Levels: S M L XL
  • 23. CREATING CLASSES IN S3 In [19]: attr(me, 'class') <- 'employee' print.employee <- function(e) { print(glue("{e$first_name} {e$last_name}", "has been a {e$title} at {e$company}", "since {e$start_date}", .sep = " ")) } print(me) Christopher Roach has been a Software Engineer at LinkedIn since 2013-06-01
  • 24. NON-STANDARD EVALUATION Function arguments in R are lazily evaluated In [20]: # `x` and `y` do not exist mean(runif(100), foo = x + y) 0.457389679029584
  • 25. This allows us to capture the code for each argument. And, do fun things with it! In [21]: print_expr <- function(expr, ...) { expr <- substitute(expr) params <- list(...) val <- eval(expr, params) print(glue("The value of {deparse(expr)} is {val}")) } print_expr(x + y, x = 4, y = 12) The value of x + y is 16
  • 26. In [23]: library(ggplot2) ggplot(mtcars, mapping = aes(x = hp, y = mpg)) + geom_point()
  • 27. COMBINING PYTHON AND R Since this is an R notebook, and I want to demonstrate how you can access the power of R from Python, we need to switch to another notebook. To see how you can incorporate R into your normal Python and Jupyter workflow, read over . this notebook (https://github.com/croach/pydata_nyc_2017/blob/master/notebooks/rpy2.ipynb)