SlideShare a Scribd company logo
Getting started with R

     Jacob van Etten
     Alberto Labarga
What is R?
“R is a free software environment for
statistical computing and graphics.”

www.r-project.org
Why R?

R takes time to learn and use.

  So why should I bother?

There are more user-friendly
    programmes, right?
12 reasons to learn R
1. Rigour and strategy in data analysis – not
“thinking after clicking”.

2. Automatizing repeated calculations can
save time in the end.

3. A lot of stuff is simply not feasible in menu
driven software.
12 reasons to learn R
4. R is not only about software, it is also an online
community of user support and collaboration.

5. Scripts make it easy to communicate about
your problem. Important for collaborative
research!

6. Research becomes replicable when scripts are
published.
12 reasons to learn R
7. R packages represent state-of-the-art in
many academic fields.

8. Graphics in R are very good.

9. R stimulates learning – graduation from
user to developer.
12 reasons to learn R
10. R is free, which saves you money. Or R may
be the only option when budgets are
restricted.

11. R encourages to freely explore new
methods and learn about them.

12. Knowing to work with R is a valuable and
transferable skill.
It’s the long way but it’s worth it...
Resources to learn R
My two picks for you



          http://pj.freefaculty.org/R/Rtips.html
Some R packages of interest
RNCEP – Reanalysis data
clim.pact – Downscaling climate data
GhcnDaily – daily weather data
weatherData (R-Forge) – Daily weather data
and derived bioclimatic variables relevant to
plant growth
raster – gives access to WorldClim data
And a lot more here...



http://cran.r-project.org/other-docs.html
Downloading R
Choose a mirror nearby and then...




       Binaries
       “When downloading, a completely functional
       program without any installer is also often
       called program binary, or binaries (as opposed
       to the source code).”
       (Wikipedia)
And finally,
         you can download R...




When downloading has finished, run the installer
The bare R interface
RStudio makes life easier



                    Rstudio.org
Four parts
    Create a new R script: File – New – R Script



Scripts,               Workspace/history
documentation



                   Files, plots, packages
                   and help
Console
Our first code...
Type

1+1

into the script area.

Then click “Run”.

What happens?
Exercises: running code
Type a second line with another calculation
(use “-”, “/”, or “*”) and click “Run” again.

Select only one line with the mouse or Shift +
arrows. Then click “Run”.

Save your first code to a separate folder
“Rexercises”.
Following exercises
In the next exercises, we will develop a script.

Just copy every new line and add it to your script,
without erasing the previous part.

If you want to make a comment in your script,
put a # before that line. Like this:

#important to remember: use # to comment
If the exercises are a bit silly...
...that’s because you are learning.
Vector
Type a new line with the expression

1:10

in the script and run this line.

A concatenation of values is called a vector.
Making a new variable
If we send 1:10 into the console it will only
print the outcome. To “store” this vector, we
need to do the following.

                    a <- 1:10

 new variable “a”   assign   vector values 1 to 10
Operations with vectors
Try the following and see what happens.
a
a*2
a*a
b <- a * a
b
print(b)
Other ways of making vectors
d <- c(1, 6, 9)
d
class(d)
f <- LETTERS
f
class(f)

What is the difference between d and f?
Functions
Actually, we have already seen functions!
Functions consist of a name followed by one
or more arguments. Commas and brackets
complete the expression.
                    class(f)
                     c(d,f)

              name argument
Cheat sheet
When you use R, you will become familiar
with the most common functions.

If you need a less common function, there are
ways to discover the right one.

For now, use the cheat sheet to look up the
functions you need.
Getting help on functions
This will open help pages for the functions in your
browser.

?c
?class

Especially the examples are often helpful.
Just copy and paste the code into the console and
see with your own eyes what happens!
Matrices
We have already met the vector.
If we put two or more vector together as
columns, we get a matrix.
X <- c(1,2,3)
Y <- c(8,9,7)
Z <- c(4,2,8)
M <- cbind(X, Y, Z)
How many columns and rows does M have?
Data frames
Matrices must consist of values of the same class. But often
datasets consist of a mix of different types of variables (real
numbers and groups). This is the job of data frames.

L <- c(“a”, “b”, “c”)
Df <- data.frame(X,Y,Z,L)

Visualize Df like this:
str(Df)

What would happen if you tried to make a matrix out of
these same vectors instead? Try and see.
Getting data into R
?read.csv

CSV files are a relatively trouble-free way of
getting data into R.

It is a fairly common format.

You can make a CSV file in any spreadsheet
software.
Create a CSV file
First name       Family name      Sex         Age
John             Travolta         Male        57
Elijah           Wood             Male        30
Nicole           Kidman           Female      44
Keira            Knightley        Female      26


    Add your own favorite actor, too.

    Open the file with Notepad.

    Make sure the values are separated by commas.
Now use R to read it

Now read it into R.
actors <- read.csv(yourfile.csv)
str(actors)
Subsetting
There are many ways of selecting only part of a
data frame. Observe carefully what happens.
actors[1:2,]
actors[,1:2]
actors[“Age”]
actors[c(“Name”, “Age”)]
subset(actors, Age> 40)
Now create a new data frame with the actors
younger than 45.
Graphics
The plot function makes graphs.

plot(actors[c(“sex”, “Age”)])
Summary
You now know about:

Variables
Functions
Vectors
Matrices
Data frames
Getting tabular data into R
Subsetting
Simple plotting
Time for your first fight...

More Related Content

PPTX
Introduction to Python Part-1
PDF
Chunked, dplyr for large text files
PDF
Docopt, beautiful command-line options for R, user2014
PDF
(Kpi summer school 2015) theano tutorial part1
PPTX
R Programming Tutorial for Beginners - -TIB Academy
PPT
Paper talk (presented by Prof. Ludaescher), WORKS workshop, 2010
PDF
Introduction to R
PPTX
Theano tutorial
Introduction to Python Part-1
Chunked, dplyr for large text files
Docopt, beautiful command-line options for R, user2014
(Kpi summer school 2015) theano tutorial part1
R Programming Tutorial for Beginners - -TIB Academy
Paper talk (presented by Prof. Ludaescher), WORKS workshop, 2010
Introduction to R
Theano tutorial

What's hot (20)

PPT
PDF
Introduction to c++ ppt
PDF
Learn a language : LISP
PPTX
R language
PPT
Introduction to Python
PPTX
Combinators, DSLs, HTML and F#
PPTX
PPT
Standard Library Functions
PPT
Data Handling
PPTX
LISP: Introduction to lisp
KEY
Presentation R basic teaching module
PPTX
Chapter 10 data handling
PPTX
Type Casting C# - Lec4 (Workshop on C# Programming: Learn to Build)
PPTX
C# Basic - Lec1 (Workshop on C# Programming: Learn to Build)
PDF
C++ Version 2
PPTX
Pa2 session 1
PPTX
Data Type C# - Lec2 (Workshop on C# Programming: Learn to Build)
PDF
Google technical-subjects-placement-papers
PPTX
Python Data-Types
Introduction to c++ ppt
Learn a language : LISP
R language
Introduction to Python
Combinators, DSLs, HTML and F#
Standard Library Functions
Data Handling
LISP: Introduction to lisp
Presentation R basic teaching module
Chapter 10 data handling
Type Casting C# - Lec4 (Workshop on C# Programming: Learn to Build)
C# Basic - Lec1 (Workshop on C# Programming: Learn to Build)
C++ Version 2
Pa2 session 1
Data Type C# - Lec2 (Workshop on C# Programming: Learn to Build)
Google technical-subjects-placement-papers
Python Data-Types
Ad

Viewers also liked (9)

PDF
Presentando
PPT
Insertar códigos en html
PPT
Algunas interfaces de las plataformas
PPTX
Inmersión
PPT
Insertar imágenes de la red
PPTX
Meta analysis with R
PDF
Linked data in industry
PDF
My presentation at BioHackathon 2010
Presentando
Insertar códigos en html
Algunas interfaces de las plataformas
Inmersión
Insertar imágenes de la red
Meta analysis with R
Linked data in industry
My presentation at BioHackathon 2010
Ad

Similar to Introduction to r (20)

PDF
Data analysis in R
PPT
Best corporate-r-programming-training-in-mumbai
PDF
Designing A Project Using Java Programming
PPTX
Presentation on use of r statistics
PDF
R-Language-Lab-Manual-lab-1.pdf
PDF
R-Language-Lab-Manual-lab-1.pdf
PDF
R-Language-Lab-Manual-lab-1.pdf
PDF
R Traning-Session-I 21-23 May 2025 Updated Alpha.pdf
PPT
Lecture1_R Programming Introduction1.ppt
PDF
Reproducibility with R
PPT
Brief introduction to R Lecturenotes1_R .ppt
PPT
R_Language_study_forstudents_R_Material.ppt
PPT
Lecture1_R.ppt
PPT
Modeling in R Programming Language for Beginers.ppt
PPT
Lecture1_R.ppt
PPT
Lecture1 r
PPT
Basics of R
PPT
Lecture_R.ppt
PDF
Oct.22nd.Presentation.Final
Data analysis in R
Best corporate-r-programming-training-in-mumbai
Designing A Project Using Java Programming
Presentation on use of r statistics
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
R Traning-Session-I 21-23 May 2025 Updated Alpha.pdf
Lecture1_R Programming Introduction1.ppt
Reproducibility with R
Brief introduction to R Lecturenotes1_R .ppt
R_Language_study_forstudents_R_Material.ppt
Lecture1_R.ppt
Modeling in R Programming Language for Beginers.ppt
Lecture1_R.ppt
Lecture1 r
Basics of R
Lecture_R.ppt
Oct.22nd.Presentation.Final

More from Alberto Labarga (20)

PDF
El Salto Communities - EditorsLab 2017
PDF
Shokesu - Premio Nobel de Literatura a Bob Dylan
PDF
Genome visualization challenges
PDF
SocialLearning: descubriendo contenidos educativos de manera colaborativa
PDF
Hacksanfermin 2015 :: Dropcoin Street
PDF
hacksanfermin 2015 :: Parking inteligente
PDF
jpd5 big data
PDF
Vidas Contadas :: Visualizar 2015
PDF
Periodismo de datos y visualización de datos abiertos #siglibre9
PDF
myHealthHackmedicine
PDF
Big Data y Salud
PDF
Arduino: Control de motores
PDF
Entrada/salida analógica con Arduino
PDF
Práctica con Arduino: Simon Dice
PDF
Entrada/Salida digital con Arduino
PDF
Presentación Laboratorio de Fabricación Digital UPNA 2014
PDF
Conceptos de electrónica - Laboratorio de Fabricación Digital UPNA 2014
PDF
Introducción a la plataforma Arduino - Laboratorio de Fabricación Digital UPN...
PDF
Introducción a la impresión 3D
PDF
Vidas Contadas
El Salto Communities - EditorsLab 2017
Shokesu - Premio Nobel de Literatura a Bob Dylan
Genome visualization challenges
SocialLearning: descubriendo contenidos educativos de manera colaborativa
Hacksanfermin 2015 :: Dropcoin Street
hacksanfermin 2015 :: Parking inteligente
jpd5 big data
Vidas Contadas :: Visualizar 2015
Periodismo de datos y visualización de datos abiertos #siglibre9
myHealthHackmedicine
Big Data y Salud
Arduino: Control de motores
Entrada/salida analógica con Arduino
Práctica con Arduino: Simon Dice
Entrada/Salida digital con Arduino
Presentación Laboratorio de Fabricación Digital UPNA 2014
Conceptos de electrónica - Laboratorio de Fabricación Digital UPNA 2014
Introducción a la plataforma Arduino - Laboratorio de Fabricación Digital UPN...
Introducción a la impresión 3D
Vidas Contadas

Introduction to r

  • 1. Getting started with R Jacob van Etten Alberto Labarga
  • 2. What is R? “R is a free software environment for statistical computing and graphics.” www.r-project.org
  • 3. Why R? R takes time to learn and use. So why should I bother? There are more user-friendly programmes, right?
  • 4. 12 reasons to learn R 1. Rigour and strategy in data analysis – not “thinking after clicking”. 2. Automatizing repeated calculations can save time in the end. 3. A lot of stuff is simply not feasible in menu driven software.
  • 5. 12 reasons to learn R 4. R is not only about software, it is also an online community of user support and collaboration. 5. Scripts make it easy to communicate about your problem. Important for collaborative research! 6. Research becomes replicable when scripts are published.
  • 6. 12 reasons to learn R 7. R packages represent state-of-the-art in many academic fields. 8. Graphics in R are very good. 9. R stimulates learning – graduation from user to developer.
  • 7. 12 reasons to learn R 10. R is free, which saves you money. Or R may be the only option when budgets are restricted. 11. R encourages to freely explore new methods and learn about them. 12. Knowing to work with R is a valuable and transferable skill.
  • 8. It’s the long way but it’s worth it...
  • 9. Resources to learn R My two picks for you http://pj.freefaculty.org/R/Rtips.html
  • 10. Some R packages of interest RNCEP – Reanalysis data clim.pact – Downscaling climate data GhcnDaily – daily weather data weatherData (R-Forge) – Daily weather data and derived bioclimatic variables relevant to plant growth raster – gives access to WorldClim data
  • 11. And a lot more here... http://cran.r-project.org/other-docs.html
  • 13. Choose a mirror nearby and then... Binaries “When downloading, a completely functional program without any installer is also often called program binary, or binaries (as opposed to the source code).” (Wikipedia)
  • 14. And finally, you can download R... When downloading has finished, run the installer
  • 15. The bare R interface
  • 16. RStudio makes life easier Rstudio.org
  • 17. Four parts Create a new R script: File – New – R Script Scripts, Workspace/history documentation Files, plots, packages and help Console
  • 18. Our first code... Type 1+1 into the script area. Then click “Run”. What happens?
  • 19. Exercises: running code Type a second line with another calculation (use “-”, “/”, or “*”) and click “Run” again. Select only one line with the mouse or Shift + arrows. Then click “Run”. Save your first code to a separate folder “Rexercises”.
  • 20. Following exercises In the next exercises, we will develop a script. Just copy every new line and add it to your script, without erasing the previous part. If you want to make a comment in your script, put a # before that line. Like this: #important to remember: use # to comment
  • 21. If the exercises are a bit silly... ...that’s because you are learning.
  • 22. Vector Type a new line with the expression 1:10 in the script and run this line. A concatenation of values is called a vector.
  • 23. Making a new variable If we send 1:10 into the console it will only print the outcome. To “store” this vector, we need to do the following. a <- 1:10 new variable “a” assign vector values 1 to 10
  • 24. Operations with vectors Try the following and see what happens. a a*2 a*a b <- a * a b print(b)
  • 25. Other ways of making vectors d <- c(1, 6, 9) d class(d) f <- LETTERS f class(f) What is the difference between d and f?
  • 26. Functions Actually, we have already seen functions! Functions consist of a name followed by one or more arguments. Commas and brackets complete the expression. class(f) c(d,f) name argument
  • 27. Cheat sheet When you use R, you will become familiar with the most common functions. If you need a less common function, there are ways to discover the right one. For now, use the cheat sheet to look up the functions you need.
  • 28. Getting help on functions This will open help pages for the functions in your browser. ?c ?class Especially the examples are often helpful. Just copy and paste the code into the console and see with your own eyes what happens!
  • 29. Matrices We have already met the vector. If we put two or more vector together as columns, we get a matrix. X <- c(1,2,3) Y <- c(8,9,7) Z <- c(4,2,8) M <- cbind(X, Y, Z) How many columns and rows does M have?
  • 30. Data frames Matrices must consist of values of the same class. But often datasets consist of a mix of different types of variables (real numbers and groups). This is the job of data frames. L <- c(“a”, “b”, “c”) Df <- data.frame(X,Y,Z,L) Visualize Df like this: str(Df) What would happen if you tried to make a matrix out of these same vectors instead? Try and see.
  • 31. Getting data into R ?read.csv CSV files are a relatively trouble-free way of getting data into R. It is a fairly common format. You can make a CSV file in any spreadsheet software.
  • 32. Create a CSV file First name Family name Sex Age John Travolta Male 57 Elijah Wood Male 30 Nicole Kidman Female 44 Keira Knightley Female 26 Add your own favorite actor, too. Open the file with Notepad. Make sure the values are separated by commas.
  • 33. Now use R to read it Now read it into R. actors <- read.csv(yourfile.csv) str(actors)
  • 34. Subsetting There are many ways of selecting only part of a data frame. Observe carefully what happens. actors[1:2,] actors[,1:2] actors[“Age”] actors[c(“Name”, “Age”)] subset(actors, Age> 40) Now create a new data frame with the actors younger than 45.
  • 35. Graphics The plot function makes graphs. plot(actors[c(“sex”, “Age”)])
  • 36. Summary You now know about: Variables Functions Vectors Matrices Data frames Getting tabular data into R Subsetting Simple plotting
  • 37. Time for your first fight...