02 the r language part 1
TRANSCRIPT
-
7/24/2019 02 the R Language Part 1
1/120
2014 RStudio, Inc. All rights reserved. Follow @rstudioapp
All Training materials are provided "as is" and
without warranty and RStudio disclaims any and all
express and implied warranties including without
limitation the implied warranties of title, fitness for
a particular purpose, merchantability and
noninfringement.
http://twitter.com/rstudioapp -
7/24/2019 02 the R Language Part 1
2/120
Studio
2014 RStudio, Inc. Follow @rstudioapp
Garrett Grolemund
Master Instructor, RStudio
August 2014
The R Language (1 of 2)Understand how R storesand works with data
http://twitter.com/rstudioapp -
7/24/2019 02 the R Language Part 1
3/120
2014 RStudio, In
1. RStudio basics
2. R basics
3. Data structures
4. Data types
Studio
-
7/24/2019 02 the R Language Part 1
4/120
RStudio
-
7/24/2019 02 the R Language Part 1
5/120 2014 RStudio, In
Studio
Open RStudio as you would any program. It'sicon looks like this
Getting started
-
7/24/2019 02 the R Language Part 1
6/120 2014 RStudio, In
Studio
The RStudio window is divided into 3-4 panes
Each keeps track of separate information.
-
7/24/2019 02 the R Language Part 1
7/120 2014 RStudio, In
Studio
The console gives
you a place to
execute commands
written in the R
computer language
R Console
-
7/24/2019 02 the R Language Part 1
8/120 2014 RStudio, In
Studio
Type commands on
the line that begins
with a > sign (known
as the prompt)
Command
prompt
-
7/24/2019 02 the R Language Part 1
9/120 2014 RStudio, In
Studio
When you hit enter,
RStudio will run
your command and
display any outputbelow it
Output
OutputNew prompt
-
7/24/2019 02 the R Language Part 1
10/120 2014 RStudio, In
Studio
As you enter
commands, you
accumulate a
history of pastcommands
History
d
-
7/24/2019 02 the R Language Part 1
11/120
2014 RStudio, In
Studio
[1]
R displays an index
next to the output.
Just ignore this.
Somewhat helpfulwhen R returns more
than one value in the
output.
S di
-
7/24/2019 02 the R Language Part 1
12/120
2014 RStudio, In
Studio
5 + 5
# 10
4 - 1
# 3
1 * 2
# 2
4 ^ 2
# 16
R
R is like a fancy calculato
on your computer
S di
-
7/24/2019 02 the R Language Part 1
13/120
2014 RStudio, In
Studio
a
-
7/24/2019 02 the R Language Part 1
14/120
2014 RStudio, In
Studio
round(3.1415)
# 3
factorial(3)
# 6
sqrt(9)
# 3
R
And it has functions that
you do more sophisticate
manipulations
3! = 3 x 2
square ro
-
7/24/2019 02 the R Language Part 1
15/120
2014 RStudio, In
Your turn
What do you think this will return?
You have 30 seconds.
factorial(round(3.1415) + 1)
-
7/24/2019 02 the R Language Part 1
16/120
2014 RStudio, In
Your turn
What do you think this will return?
You have 30 seconds.
factorial(round(2.0015) + 1)
Studio
-
7/24/2019 02 the R Language Part 1
17/120
2014 RStudio, In
Studio
factorial(round(2.0015) + 1)
factorial(2 + 1)
factorial(3)
6
R always works from the innermost parenthesi
the outermost (just like a calculator).
Studio
-
7/24/2019 02 the R Language Part 1
18/120
2014 RStudio, In
Studio
factorial(round(2.0015)+ 1)
factorial(2+ 1)
factorial(3)
6
R always works from the innermost parenthesi
the outermost (just like a calculator).
Studio
-
7/24/2019 02 the R Language Part 1
19/120
2014 RStudio, In
Studio
factorial(round(2.0015) + 1)
factorial(2 + 1)
factorial(3)
6
R always works from the innermost parenthesi
the outermost (just like a calculator).
Studio
-
7/24/2019 02 the R Language Part 1
20/120
2014 RStudio, In
Studio
factorial(round(2.0015) + 1)
factorial(2 + 1)
factorial(3)
63! = 3 x 2 x 1
R always works from the innermost parenthesi
the outermost (just like a calculator).
Studio
-
7/24/2019 02 the R Language Part 1
21/120
2014 RStudio, In
Studio
+ prompt
If your prompt turns
into a "+", R thinks
you haven't finished
your previous
command.Either finish the
command, or press
escape.
Studio
-
7/24/2019 02 the R Language Part 1
22/120
2014 RStudio, In
+ prompt
If your prompt turns
into a "+", R thinks
you haven't finished
your previous
command.Either finish the
command, or press
escape.
-
7/24/2019 02 the R Language Part 1
23/120
2014 RStudio, In
Your turn
Open RStudio and try the following tasks:
1. Pick a number and add 2 to it
2. Multiply the result by 3
3. Subtract 6 from the result of step 24. Divide the result of step 3 by 3
Studio
-
7/24/2019 02 the R Language Part 1
24/120
2014 RStudio, In
10 + 2
# 12
12 * 3
# 36
36 - 6
# 30
30 / 3
# 10
-
7/24/2019 02 the R Language Part 1
25/120
Workflow
Studio
-
7/24/2019 02 the R Language Part 1
26/120
2014 RStudio, In
R scripts
It is easier to compose your code in an Rscript than in the command line.
To open an R script, go to File>New File>R
Script in the toolbar.
-
7/24/2019 02 the R Language Part 1
27/120
2014 RStudio, In
R Script pane
-
7/24/2019 02 the R Language Part 1
28/120
2014 RStudio, In
1.Write code in an R
script
Common
workflow
-
7/24/2019 02 the R Language Part 1
29/120
2014 RStudio, In
1.Write code in an R
script
2.Run code in
console with Run
Common
workflow
-
7/24/2019 02 the R Language Part 1
30/120
2014 RStudio, In
1.Write code in an R
script
2.Run code in
console with Run3.Save R Script
when finished
Common
workflow
-
7/24/2019 02 the R Language Part 1
31/120
Shortcuts
-
7/24/2019 02 the R Language Part 1
32/120
-
7/24/2019 02 the R Language Part 1
33/120
[]
-
7/24/2019 02 the R Language Part 1
34/120
[Tab]
-
7/24/2019 02 the R Language Part 1
35/120
[Cmd + ]
-
7/24/2019 02 the R Language Part 1
36/120
-
7/24/2019 02 the R Language Part 1
37/120
[Cmd + enter]
-
7/24/2019 02 the R Language Part 1
38/120
R objects
Studio
-
7/24/2019 02 the R Language Part 1
39/120
2014 RStudio, In
foo
-
7/24/2019 02 the R Language Part 1
40/120
2014 RStudio, In
foo
-
7/24/2019 02 the R Language Part 1
41/120
2014 RStudio, In
foo
-
7/24/2019 02 the R Language Part 1
42/120
2014 RStudio, In
foo
-
7/24/2019 02 the R Language Part 1
43/120
2014 RStudio, In
When you create an R object, you'll see it appe
in your workspace pane
Studio
-
7/24/2019 02 the R Language Part 1
44/120
2014 RStudio, In
foo
-
7/24/2019 02 the R Language Part 1
45/120
2014 RStudio, In
a
b
FOO
my_var
.day
Object names
Object names cannot begin with numbers.
Wise to avoid names already in use.
1trial
$
^mean
2nd
!bad
Studio
-
7/24/2019 02 the R Language Part 1
46/120
2014 RStudio, In
a
b
sum
Capitalization matters
R will treat each of these as a di"erent
object
A
B
SUM
Studio
-
7/24/2019 02 the R Language Part 1
47/120
2014 RStudio, In
foo
# 4
rm(foo)
foo
# Error: object 'foo' not found
rm
You can remove an object with rm
Studio
-
7/24/2019 02 the R Language Part 1
48/120
2014 RStudio, In
This can be useful if you overwrite an object that
comes with R
pi# 3.141593
pi
-
7/24/2019 02 the R Language Part 1
49/120
Data
structures
W
-
7/24/2019 02 the R Language Part 1
50/120
2014 RStudio, In
Warm up
Look at the R object WorldPhones(by
typing its name in the console).
What is inside of WorldPhones?
WorldPhones
-
7/24/2019 02 the R Language Part 1
51/120
2014 RStudio, In
N.Amer Europe Asia S.Amer Oceania Africa Mid.Amer
1951 45939 21574 2876 1815 1646 89 555
1956 60423 29990 4708 2568 2366 1411 733
1957 64721 32510 5230 2695 2526 1546 773
1958 68484 35218 6662 2845 2691 1663 836
1959 71799 37598 6856 3000 2868 1769 911
1960 76036 40341 8220 3145 3054 1905 1008
1961 79831 43173 9053 3338 3224 2005 1076
WorldPhones
You can save more than a single number in an
object by creating a vector,matrix, orarray.
Studio
-
7/24/2019 02 the R Language Part 1
52/120
2014 RStudio, In
vec
-
7/24/2019 02 the R Language Part 1
53/120
2014 RStudio, In
vec
-
7/24/2019 02 the R Language Part 1
54/120
2014 RStudio, In
mat
-
7/24/2019 02 the R Language Part 1
55/120
2014 RStudio, In
mat
-
7/24/2019 02 the R Language Part 1
56/120
2014 RStudio, In
matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
# [,1] [,2] [,3]
# [1,] 1 3 5
# [2,] 2 4 6
vector of elements to
go in the matrix
Studio
-
7/24/2019 02 the R Language Part 1
57/120
2014 RStudio, In
matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
# [,1] [,2] [,3]
# [1,] 1 3 5
# [2,] 2 4 6
number of rows for
matrix
Studio
-
7/24/2019 02 the R Language Part 1
58/120
2014 RStudio, In
matrix(c(1, 2, 3, 4, 5, 6), nrow = 3)
# [,1] [,2]
# [1,] 1 4
# [2,] 2 5
# [3,] 3 6
Studio
-
7/24/2019 02 the R Language Part 1
59/120
2014 RStudio, In
vec + 4
# 5 6 7 14 104
vec * 4
# 4 8 12 40 400
vec * vec
# 1 4 9 100 10000
Math: element-wise
Studio
-
7/24/2019 02 the R Language Part 1
60/120
2014 RStudio, In
vec * vec
# 1 4 9 100 10000
vec
1
2
3
10
100
vec
1
2
3
10
100
1
4
9
100
10000
*
*
*
*
*
=
=
=
=
=
Studio
-
7/24/2019 02 the R Language Part 1
61/120
2014 RStudio, In
vec %*% vec # inner
# [,1]
# [1,] 10114
vec %o% vec # outer
# [,1] [,2] [,3] [,4] [,5]
# [1,] 1 2 3 10 100
# [2,] 2 4 6 20 200
# [3,] 3 6 9 30 300
# [4,] 10 20 30 100 1000
# [5,] 100 200 300 1000 10000
Matrix multiplication
Studio
-
7/24/2019 02 the R Language Part 1
62/120
2014 RStudio, In
mat
# [,1] [,2] [,3]
# [1,] 1 3 5
# [2,] 2 4 6
t(mat)
# [,1] [,2]
# [1,] 1 2
# [2,] 3 4
# [3,] 5 6
Studio
-
7/24/2019 02 the R Language Part 1
63/120
2014 RStudio, In
array(c(1, 2, 3, 4, 5, 6), dim = c(2, 2, 3
arrays
Combine multiple elements into an array
that has three or more dimensions.
Create with the arrayfunction.
Studio
-
7/24/2019 02 the R Language Part 1
64/120
2014 RStudio, In
array(c(1, 2, 3, 4, 5, 6), dim = c(2, 2, 3
arrays
Combine multiple elements into an array
that has three or more dimensions.
Create with the arrayfunction.
Wewo
o
-
7/24/2019 02 the R Language Part 1
65/120
Data types
Warm up
-
7/24/2019 02 the R Language Part 1
66/120
2014 RStudio, In
Wha
ttyp
esofd
inthi
sspr
d t t
Studio
-
7/24/2019 02 the R Language Part 1
67/120
2014 RStudio, In
data t esLike Excel, Numbers, etc., R can recognize
di"erent types of data.
We'll look at four basic types:
# numbers
# character strings (text)
# logical
# factor
numeric
Studio
-
7/24/2019 02 the R Language Part 1
68/120
2014 RStudio, In
numeric
Any number, no quotes.
Appropriate for math.
1 + 1
3000000
class(0.00001)
# "numeric"
character
Studio
-
7/24/2019 02 the R Language Part 1
69/120
2014 RStudio, In
character
Any symbols surrounded by quotes.
Appropriate for words, variable names,
messages, any text.
"hello"
class("hello")
# "character"
Studio
-
7/24/2019 02 the R Language Part 1
70/120
2014 RStudio, In
"hello" + "world"
# Error
nchar("hello")
# 5
paste("hello", "world")
# "hello world"
Warm up
-
7/24/2019 02 the R Language Part 1
71/120
2014 RStudio, In
Warm up
Which of these are numbers? What
are the others?How can you tell?
1 "1" "one"
logical
Studio
-
7/24/2019 02 the R Language Part 1
72/120
2014 RStudio, In
logical
TRUE or FALSE
R's form of binary data. Useful for logical tests
3 < 4
# TRUE
class(TRUE)
# "logical"
class(T)
# "logical"
factor
Studio
-
7/24/2019 02 the R Language Part 1
73/120
2014 RStudio, In
factorR's form of categorical data. Saved as an
integer with a set of labels (e.g. levels).
fac
-
7/24/2019 02 the R Language Part 1
74/120
2014 RStudio, In
Quiz
x
-
7/24/2019 02 the R Language Part 1
75/120
2014 RStudio, In
Aside: dates and times
Studio
-
7/24/2019 02 the R Language Part 1
76/120
2014 RStudio, In
s de da es a d es
Surprisingly di$cult for computers!
We won't cover, but I recommend the
following resources
http://www.r-statistics.com/2012/03/do-more-
with-dates-and-times-in-r-with-lubridate-1-1-0/
http://www.jstatsoft.org/v40/i03/
Your turn
http://www.jstatsoft.org/v40/i03/http://www.r-statistics.com/2012/03/do-more-with-dates-and-times-in-r-with-lubridate-1-1-0/ -
7/24/2019 02 the R Language Part 1
77/120
2014 RStudio, In
Your turn
Make a vector that contains the number 1,
the letter R, and the logical TRUE.
What class of data is the vector?
Studio
-
7/24/2019 02 the R Language Part 1
78/120
2014 RStudio, In
vec
-
7/24/2019 02 the R Language Part 1
79/120
2014 RStudio, In
Vector
V t 1 2 3
-
7/24/2019 02 the R Language Part 1
80/120
2014 RStudio, In
Vector 1 2 3
Vector 1 2 3
-
7/24/2019 02 the R Language Part 1
81/120
2014 RStudio, In
Vector 1 2 3
numeric
Vector "a" "R" "b"
-
7/24/2019 02 the R Language Part 1
82/120
2014 RStudio, In
Vector a "R" "b"
character
Vector TRUE TRUE TRUE
-
7/24/2019 02 the R Language Part 1
83/120
2014 RStudio, In
Vector TRUE TRUE TRUE
logical
Vector 1 "R" TRUE
-
7/24/2019 02 the R Language Part 1
84/120
2014 RStudio, In
Vector 1 R TRUE
?
Vector "1" "R" "TRUE"
-
7/24/2019 02 the R Language Part 1
85/120
2014 RStudio, In
Vector 1 R TRUE
character
coercion
Studio
-
7/24/2019 02 the R Language Part 1
86/120
2014 RStudio, In
logical
numeric
character
coercion
Studio
-
7/24/2019 02 the R Language Part 1
87/120
2014 RStudio, In
logical
numeric
character
TRUE
FALSE
1
0
Quiz
-
7/24/2019 02 the R Language Part 1
88/120
2014 RStudio, In
What type of data will result?
c(5, "two")
c(TRUE, "a")
c(1, "TRUE")
TRUE + 5
Quiz
-
7/24/2019 02 the R Language Part 1
89/120
2014 RStudio, In
What type of data will result?
c(5, "two")
c(TRUE, "a")
c(1, "TRUE")
TRUE + 5
character
Quiz
-
7/24/2019 02 the R Language Part 1
90/120
2014 RStudio, In
What type of data will result?
c(5, "two")
c(TRUE, "a")
c(1, "TRUE")
TRUE + 5
character
character
Quiz
-
7/24/2019 02 the R Language Part 1
91/120
2014 RStudio, In
What type of data will result?c(5, "two")
c(TRUE, "a")
c(1, "TRUE")
TRUE + 5
character
character
character
Quiz
-
7/24/2019 02 the R Language Part 1
92/120
2014 RStudio, In
What type of data will result?c(5, "two")
c(TRUE, "a")
c(1, "TRUE")
TRUE + 5
character
character
character
Quiz
-
7/24/2019 02 the R Language Part 1
93/120
2014 RStudio, In
What type of data will result?c(5, "two")
c(TRUE, "a")
c(1, "TRUE")
TRUE + 5
character
character
numeric
character
Studio
manual coercion
-
7/24/2019 02 the R Language Part 1
94/120
2014 RStudio, In
as.numeric("1")
as.character(TRUE)
Matrix 1 "R" TRUE
-
7/24/2019 02 the R Language Part 1
95/120
2014 RStudio, In
?
2
3
"S"
"T"
FALSE
TRUE
Matrix "1" "R" "TRUE"
-
7/24/2019 02 the R Language Part 1
96/120
2014 RStudio, In
character
"2"
"3"
"S"
"T"
"FALSE"
"TRUE"
Matrix "1" "R" "TRUE"
-
7/24/2019 02 the R Language Part 1
97/120
2014 RStudio, In
character
"2"
"3"
"S"
"T"
"FALSE"
"TRUE"
What if you want di"erent datatypes in the same object?
lists and data frames
Studio
-
7/24/2019 02 the R Language Part 1
98/120
2014 RStudio, In
listsand data framesgeneralize vectors and
matrices to allow multiple types of data
lists
Studio
-
7/24/2019 02 the R Language Part 1
99/120
2014 RStudio, In
A list is a one dimensional group of R
objects.Create lists with list
lst
-
7/24/2019 02 the R Language Part 1
100/120
2014 RStudio, In
List
character
Vector "1" "R" "TRUE"
-
7/24/2019 02 the R Language Part 1
101/120
2014 RStudio, In
List
character
1
Vector "1" "R" "TRUE"
-
7/24/2019 02 the R Language Part 1
102/120
2014 RStudio, In
List
character
numeric
1
Vector "1" "R" "TRUE"
-
7/24/2019 02 the R Language Part 1
103/120
2014 RStudio, In
List
character
numeric
1 "R"
Vector "1" "R" "TRUE"
-
7/24/2019 02 the R Language Part 1
104/120
2014 RStudio, In
List
character
characternumeric
1 "R"
Vector "1" "R" "TRUE"
-
7/24/2019 02 the R Language Part 1
105/120
2014 RStudio, In
List
character
characternumeric
1 "R" TRUE
Vector "1" "R" "TRUE"
-
7/24/2019 02 the R Language Part 1
106/120
2014 RStudio, In
List
character
characternumeric logical
1 "R" TRUE
The elements of a list can be anything. Even
vectors or other lists
-
7/24/2019 02 the R Language Part 1
107/120
2014 RStudio, In
List c(1, 2) c("a", "b", "c")TRUE
list(c(1, 2), TRUE, c("a", "b", "c"))
vectors or other lists.
data frame
Studio
-
7/24/2019 02 the R Language Part 1
108/120
2014 RStudio, In
A data frame is a two dimensional group of Robjects.
Each column in a data frame can be a di"erenttype
df
-
7/24/2019 02 the R Language Part 1
109/120
2014 RStudio, In
character
"3" "T" "TRUE"
data frame 1
2
3
Matrix "1" "R" "TRUE"
"2" "S" "FALSE"
-
7/24/2019 02 the R Language Part 1
110/120
2014 RStudio, In
character
"3" "T" "TRUE"
numeric
data frame 1
2
3
Matrix "1" "R" "TRUE"
"2" "S" "FALSE"
-
7/24/2019 02 the R Language Part 1
111/120
2014 RStudio, In
character
"3" "T" "TRUE"
numeric
data frame 1 "R"
2
3
"S"
"T"
Matrix "1" "R" "TRUE"
"2" "S" "FALSE"
-
7/24/2019 02 the R Language Part 1
112/120
2014 RStudio, In
character
"3" "T" "TRUE"
numeric character
data frame 1 "R"
2
3
"S"
"T"
Matrix "1" "R" "TRUE"
"2" "S" "FALSE"
-
7/24/2019 02 the R Language Part 1
113/120
2014 RStudio, In
character
"3" "T" "TRUE"
numeric character
data frame 1 "R" TRUE
2
3
"S"
"T"
FALSE
TRUE
Matrix "1" "R" "TRUE"
"2" "S" "FALSE"
-
7/24/2019 02 the R Language Part 1
114/120
2014 RStudio, In
character
"3" "T" "TRUE"
numeric character logical
data frame 1 "R" TRUE
2
3
"S"
"T"
FALSE
TRUE
names
Studio
-
7/24/2019 02 the R Language Part 1
115/120
2014 RStudio, In
You can name the elements of a vector, list, or
data frame when you create them.
nvec
-
7/24/2019 02 the R Language Part 1
116/120
2014 RStudio, In
nlst
# $one
# [1] 1
#
# $two
# [1] 2#
# $many
# [1] 3 4 5
ndf
-
7/24/2019 02 the R Language Part 1
117/120
2014 RStudio, In
logic = c(TRUE, FALSE, TRUE
ndf
# numbers letters logic
# 1 1 R TRUE
# 2 2 S FALSE# 3 3 T TRUE
You can also see and set the names with names
Studio
-
7/24/2019 02 the R Language Part 1
118/120
2014 RStudio, In
names(ndf)
# [1] "numbers" "letters" "logic"
names(nvec)
# [1] "one" "two" "three"
names(nvec)
-
7/24/2019 02 the R Language Part 1
119/120
2014 RStudio, In
Matrix
Array
Data frame
2D
nD
Studio
helper functions for data structures
-
7/24/2019 02 the R Language Part 1
120/120
2014 RStudio, In