SlideShare a Scribd company logo
A simple introduction to R
for market researchers
October
2019
Webinar Friday 4 October
Live broadcast 10am New York (3pm London)
Ray Poynter
Chief Research Officer, Potentiate
What is R?
• An open-source, free statistical language
• The core language is expanded by an enormous collection of
libraries
• Available for Windows, Mac and UNIX
• Find out about R (& download it) from: -
https://www.r-project.org/
• Learning R
• Books
• Articles
• Videos
• E-learning, e.g. DataCamp, Udemy & Coursera
What is RStudio?
• There are other choices, but nearly everybody I know
is using RStudio to work with R
• It is an IDE (Integrated Development Environment)
• Editor
• A tidy place to run R, to see the variables, and keep things tidy
• There are open-source and commercial options (free and
not-free)
• Find out more and download it from https://rstudio.com/
Commands and R
Commands are entered in the Console window
Commands and R – Hello World
> print("Hello World")
[1] "Hello World"
>
> print("Hello World", quote=FALSE)
[1] Hello World
>
> myText <- "Hello World"
> print(myText)
[1] "Hello World"
>
> myText
[1] "Hello World"
>
Commands and R – Variables
> a <- 2
> b <- 4
> print(a * b)
[1] 8
>
> c <- a * b
> c
[1] 8
>
> c <- "Hello World"
> c
[1] "Hello World"
>
Commands and R – Vectors
> x <- c(1,2,3,4)
> x
[1] 1 2 3 4
> y <- 2 * x
> y
[1] 2 4 6 8
> z <- c("One","Two","Three")
> z
[1] "One" "Two" "Three"
Commands and R – Data sets
R has lots of built in data sets. For example, mtcars
> str(mtcars)
'data.frame': 32 obs. of 11 variables:
$ mpg :Class 'labelled' num 21 21 22.8 21.4 18.7 18.1 14.3
24.4 22.8 19.2 ...
.. .. LABEL: Miles/(US) gallon
$ cyl :Class 'labelled' num 6 6 4 6 8 6 8 4 4 6 ...
.. .. LABEL: Number of cylinders
And 9 more variables
mtcars
Use help (or ?) to understand an included data set
> ?mtcars
mtcars {datasets} R Documentation
Motor Trend Car Road Tests
Description
The data was extracted from the 1974 Motor Trend US magazine, and
comprises fuel consumption and 10 aspects of automobile design and
performance for 32 automobiles (1973–74 models).
Usage
Mtcars
Format
A data frame with 32 observations on 11 (numeric) variables.
Commands and R – Data Frames
Data Frame – a compound structure,
where the rows can be different sorts of items.
> head(mtcars,4)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Commands and R – Frames and Vectors
We can address vectors from inside a data frame using $
> summary(mtcars$mpg)
Min. 1st Qu. Median Mean 3rd Qu. Max.
10.40 15.43 19.20 20.09 22.80 33.90
>
Commands and R – simple chart
> hist(mtcars$mpg)
Commands and R - libraries
The real power of R comes from the installed libraries
> install.packages("ggplot2")
trying URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.6/ggplot2_3.2.1.tgz'
Content type 'application/x-gzip' length 3973186 bytes (3.8 MB)
==================================================
downloaded 3.8 MB
The downloaded binary packages are in
/var/folders/wp/n9tjrcps0990gpznfmpqff2h0000gn/T//RtmpXDhlrV/downloaded_packages
> library(ggplot2)
>
> ggplot(mtcars, aes(x=hp, y=mpg)) +
geom_point(aes(shape=factor(cyl), colour=factor(cyl))) +
xlab("Performance (horse power)") +
ylab("Fuel consumption (mpg)")+
ggtitle("More cylinders are associated with fewer miles per gallon") +
scale_shape_discrete(name="Cylinders") +
scale_colour_discrete(name="Cylinders")
ggplot2 example
Scripts and R
Scripts are the best way to use R
• You create a record of what you did
• You can tweak the code
• You can audit the code
• You can re-use the code for other projects
• Comment your code to make it readable
Scripts and R
Console
From the Script
1. Run the whole script
2. Select and run a section
3. Run a single line
The code and the results appear in
the Console
Scripts and Charting Example
Plots go in the plot window
and can be exported
Tables and R
The Iris Data Set
> data(iris)
> ?iris
Edgar Anderson's Iris Data
Description
This famous (Fisher's or Anderson's) iris data set gives the measurements in
centimeters of the variables sepal length and width and petal length and width,
respectively, for 50 flowers from each of 3 species of iris. The species are Iris
setosa, versicolor, and virginica.
iris is a data frame with 150 cases (rows) and 5 variables (columns)
named Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, and Species.
Principal Components and Charting Example
> head(pca_mod,2)
$sdev
[1] 2.0562689 0.4926162 0.2796596 0.1543862
$rotation
PC1 PC2 PC3 PC4
Sepal.Length 0.36138659 -0.65658877 0.58202985 0.3154872
Sepal.Width -0.08452251 -0.73016143 -0.59791083 -0.3197231
Petal.Length 0.85667061 0.17337266 -0.07623608 -0.4798390
Petal.Width 0.35828920 0.07548102 -0.54583143 0.7536574
Principal Components
and Charting Example
Principal Components
and Charting Example
Text tools
This example uses ‘The Wonderdul Wizard of Oz, by L. Frank Baum
downloaded from Project Gutenberg http://www.gutenberg.org/ebooks/55
Tidy the text
Bar chart of common words
Word cloud
Digging deeper
Overview
• Free and open-source
• Massive collection of libraries
• Stats
• Text analytics
• AI tools
• Graphics
• Relatively steep learning curve
• More about finding the story than telling the story
• Lots of resources for learning about R
• Books, videos, courses, papers etc
Q & A
Ray Poynter
Chief Research Officer
Potentiate
October
2019
#NewMR Sponsors
October
2019
Communication
Gold
Silver

More Related Content

PPTX
R program
PDF
Herve_Momo-TASS_25SEP2015
DOCX
PPT
A brief introduction to 'R' statistical package
PPTX
Python pandas Library
PPTX
Ds stack & queue
PPTX
Pandas
PDF
Unit1_Introduction to R.pdf
R program
Herve_Momo-TASS_25SEP2015
A brief introduction to 'R' statistical package
Python pandas Library
Ds stack & queue
Pandas
Unit1_Introduction to R.pdf

Similar to A Simple Introduction to R for Market Researchers (20)

PPT
An introduction to R is a document useful
PPTX
2015-10-23_wim_davis_r_slides.pptx on consumer
PPTX
RPreliminariesdsjhfsdsfhjshfjsdhjfhjfhdfjhf
PPT
Introduction to r language programming.ppt
PDF
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
PPT
Basics of R
PDF
R programming for data science
PDF
01_introduction_lab.pdf
PPTX
Getting Started with R
PPTX
R and Rcmdr Statistical Software
PDF
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
PPTX
Exploratory Data Analysis
PDF
R-Language-Lab-Manual-lab-1.pdf
PDF
R-Language-Lab-Manual-lab-1.pdf
PDF
R-Language-Lab-Manual-lab-1.pdf
PPT
Introduction to R for Data Science Technology
PPT
How to obtain and install R.ppt
PPTX
Basic Analytic Techniques - Using R Tool - Part 1
PPT
PPT
Slides on introduction to R by ArinBasu MD
An introduction to R is a document useful
2015-10-23_wim_davis_r_slides.pptx on consumer
RPreliminariesdsjhfsdsfhjshfjsdhjfhjfhdfjhf
Introduction to r language programming.ppt
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
Basics of R
R programming for data science
01_introduction_lab.pdf
Getting Started with R
R and Rcmdr Statistical Software
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
Exploratory Data Analysis
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
Introduction to R for Data Science Technology
How to obtain and install R.ppt
Basic Analytic Techniques - Using R Tool - Part 1
Slides on introduction to R by ArinBasu MD
Ad

More from Ray Poynter (20)

PDF
The State of AI in Insights and Research 2024: Results and Findings
PDF
ResearchWiseAI - an artificial intelligence driven research data analysis tool
PDF
AI-powered interviewing: Best practices from Yasna
PDF
Artificial Intelligence and Qual: The Story So Far
PDF
State of Research Insights in Q1, 2024 from NewMR
PDF
Sudden Death of Beliefs
PDF
Uncovering Consumers’ Hidden Narratives
PDF
Narrative Exploration of New Categories at Mondelēz
PDF
The Future in Focus
PDF
The Future in Focus
PDF
The State of Insights – September 2023
PDF
Research Thinking in the age of AI
PDF
How might AI impact Research and Insights over the next two years?
PDF
From Words to Wisdom: Unleashing the Potential of Language Models for Human-C...
PDF
ChatGPT for Social Media Listening: practical application with YouScan’s Insi...
PDF
Using Generative AI to Assess the Quality of Open-Ended Responses in Surveys
PDF
Exploring the future of verbatim coding with ChatGPT
PDF
Using Generative AI to bring Qualitative Capabilities to Quantitative Surveys
PDF
How AI / ChatGPT Drives Business Growth
PDF
Tech for tech’s sake? Learnings from experiments with AI in consumer research
The State of AI in Insights and Research 2024: Results and Findings
ResearchWiseAI - an artificial intelligence driven research data analysis tool
AI-powered interviewing: Best practices from Yasna
Artificial Intelligence and Qual: The Story So Far
State of Research Insights in Q1, 2024 from NewMR
Sudden Death of Beliefs
Uncovering Consumers’ Hidden Narratives
Narrative Exploration of New Categories at Mondelēz
The Future in Focus
The Future in Focus
The State of Insights – September 2023
Research Thinking in the age of AI
How might AI impact Research and Insights over the next two years?
From Words to Wisdom: Unleashing the Potential of Language Models for Human-C...
ChatGPT for Social Media Listening: practical application with YouScan’s Insi...
Using Generative AI to Assess the Quality of Open-Ended Responses in Surveys
Exploring the future of verbatim coding with ChatGPT
Using Generative AI to bring Qualitative Capabilities to Quantitative Surveys
How AI / ChatGPT Drives Business Growth
Tech for tech’s sake? Learnings from experiments with AI in consumer research
Ad

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
RMMM.pdf make it easy to upload and study
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Institutional Correction lecture only . . .
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
master seminar digital applications in india
PDF
Basic Mud Logging Guide for educational purpose
PDF
Computing-Curriculum for Schools in Ghana
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
Final Presentation General Medicine 03-08-2024.pptx
Cell Structure & Organelles in detailed.
RMMM.pdf make it easy to upload and study
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Institutional Correction lecture only . . .
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Pre independence Education in Inndia.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Supply Chain Operations Speaking Notes -ICLT Program
master seminar digital applications in india
Basic Mud Logging Guide for educational purpose
Computing-Curriculum for Schools in Ghana
O5-L3 Freight Transport Ops (International) V1.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
O7-L3 Supply Chain Operations - ICLT Program
Microbial diseases, their pathogenesis and prophylaxis
Abdominal Access Techniques with Prof. Dr. R K Mishra

A Simple Introduction to R for Market Researchers

  • 1. A simple introduction to R for market researchers October 2019 Webinar Friday 4 October Live broadcast 10am New York (3pm London) Ray Poynter Chief Research Officer, Potentiate
  • 2. What is R? • An open-source, free statistical language • The core language is expanded by an enormous collection of libraries • Available for Windows, Mac and UNIX • Find out about R (& download it) from: - https://www.r-project.org/ • Learning R • Books • Articles • Videos • E-learning, e.g. DataCamp, Udemy & Coursera
  • 3. What is RStudio? • There are other choices, but nearly everybody I know is using RStudio to work with R • It is an IDE (Integrated Development Environment) • Editor • A tidy place to run R, to see the variables, and keep things tidy • There are open-source and commercial options (free and not-free) • Find out more and download it from https://rstudio.com/
  • 4. Commands and R Commands are entered in the Console window
  • 5. Commands and R – Hello World > print("Hello World") [1] "Hello World" > > print("Hello World", quote=FALSE) [1] Hello World > > myText <- "Hello World" > print(myText) [1] "Hello World" > > myText [1] "Hello World" >
  • 6. Commands and R – Variables > a <- 2 > b <- 4 > print(a * b) [1] 8 > > c <- a * b > c [1] 8 > > c <- "Hello World" > c [1] "Hello World" >
  • 7. Commands and R – Vectors > x <- c(1,2,3,4) > x [1] 1 2 3 4 > y <- 2 * x > y [1] 2 4 6 8 > z <- c("One","Two","Three") > z [1] "One" "Two" "Three"
  • 8. Commands and R – Data sets R has lots of built in data sets. For example, mtcars > str(mtcars) 'data.frame': 32 obs. of 11 variables: $ mpg :Class 'labelled' num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ... .. .. LABEL: Miles/(US) gallon $ cyl :Class 'labelled' num 6 6 4 6 8 6 8 4 4 6 ... .. .. LABEL: Number of cylinders And 9 more variables
  • 9. mtcars Use help (or ?) to understand an included data set > ?mtcars mtcars {datasets} R Documentation Motor Trend Car Road Tests Description The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models). Usage Mtcars Format A data frame with 32 observations on 11 (numeric) variables.
  • 10. Commands and R – Data Frames Data Frame – a compound structure, where the rows can be different sorts of items. > head(mtcars,4) mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
  • 11. Commands and R – Frames and Vectors We can address vectors from inside a data frame using $ > summary(mtcars$mpg) Min. 1st Qu. Median Mean 3rd Qu. Max. 10.40 15.43 19.20 20.09 22.80 33.90 >
  • 12. Commands and R – simple chart > hist(mtcars$mpg)
  • 13. Commands and R - libraries The real power of R comes from the installed libraries > install.packages("ggplot2") trying URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.6/ggplot2_3.2.1.tgz' Content type 'application/x-gzip' length 3973186 bytes (3.8 MB) ================================================== downloaded 3.8 MB The downloaded binary packages are in /var/folders/wp/n9tjrcps0990gpznfmpqff2h0000gn/T//RtmpXDhlrV/downloaded_packages > library(ggplot2) > > ggplot(mtcars, aes(x=hp, y=mpg)) + geom_point(aes(shape=factor(cyl), colour=factor(cyl))) + xlab("Performance (horse power)") + ylab("Fuel consumption (mpg)")+ ggtitle("More cylinders are associated with fewer miles per gallon") + scale_shape_discrete(name="Cylinders") + scale_colour_discrete(name="Cylinders")
  • 15. Scripts and R Scripts are the best way to use R • You create a record of what you did • You can tweak the code • You can audit the code • You can re-use the code for other projects • Comment your code to make it readable
  • 16. Scripts and R Console From the Script 1. Run the whole script 2. Select and run a section 3. Run a single line The code and the results appear in the Console
  • 17. Scripts and Charting Example Plots go in the plot window and can be exported
  • 19. The Iris Data Set > data(iris) > ?iris Edgar Anderson's Iris Data Description This famous (Fisher's or Anderson's) iris data set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica. iris is a data frame with 150 cases (rows) and 5 variables (columns) named Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, and Species.
  • 20. Principal Components and Charting Example > head(pca_mod,2) $sdev [1] 2.0562689 0.4926162 0.2796596 0.1543862 $rotation PC1 PC2 PC3 PC4 Sepal.Length 0.36138659 -0.65658877 0.58202985 0.3154872 Sepal.Width -0.08452251 -0.73016143 -0.59791083 -0.3197231 Petal.Length 0.85667061 0.17337266 -0.07623608 -0.4798390 Petal.Width 0.35828920 0.07548102 -0.54583143 0.7536574
  • 23. Text tools This example uses ‘The Wonderdul Wizard of Oz, by L. Frank Baum downloaded from Project Gutenberg http://www.gutenberg.org/ebooks/55
  • 25. Bar chart of common words
  • 28. Overview • Free and open-source • Massive collection of libraries • Stats • Text analytics • AI tools • Graphics • Relatively steep learning curve • More about finding the story than telling the story • Lots of resources for learning about R • Books, videos, courses, papers etc
  • 29. Q & A Ray Poynter Chief Research Officer Potentiate October 2019