From Grolemund and Wickham, R for Data Science
echo=TRUE
to present the code or echo=FALSE
to hide it.echo=TRUE
to present the code or echo=FALSE
to hide it.a
and the number 2 in a variable b
?a<-1b=2
<-
or =
to assign variables. There are a few options for looking at what is stored in a variable
print(b)
## [1] 2
b
## [1] 2
str(b)
## num 2
name
.name<-'Anastasios'str(name)
## chr "Anastasios"
Anastasios
..
and the underscore _
?make.names
into your R consoleFirstName
First.Name
First_Name
.FirstName
1stName
.1stName
_First.Name
First Name
FirstName?
Όνομα
название
名字
이름
1Όνομα
.название
名 字
Name
name
NAME
nAMe
ls()
## [1] "a" "b" "name"
rm(list=ls())
getwd()
## [1] "/home/anastasios/Documents/Teaching/DataVizA2019/Lectures/01Intro"
setwd
setwd("/home/anastasios/Documents")
a+b
. Also we will put this in a new variable called z
.z<-a+bstr(z)
## num 3
-
, to multiply use *
, to divide /
and to take powers use ^
.sqrt
takes the square root. rootb<-sqrt(b)str(rootb)
## num 1.41
What happens when you take a square root of something that is not a number?
rootname<-sqrt(name)
## Error in sqrt(name): non-numeric argument to mathematical function
?
?sqrt
??
. Try to find a function to do logarithms using??logarithms
Anything after a #
will not be executed by R.
a<-1 # Set the variable a to 1#x<-4 This line is not executed str(a)
## num 1
str(x)
## Error in str(x): object 'x' not found
Comment multiple lines using Ctrl+Shift+C
We can create a variable with multiple numbers or strings using the c
function.
Consumption<-c(50,40,25,0)str(Consumption)
## num [1:4] 50 40 25 0
Drink<-c('Coke','Pepsi','Coke','Homebrand')str(Drink)
## chr [1:4] "Coke" "Pepsi" "Coke" "Homebrand"
These variables are example of a vector. Sometimes when we apply a function to a vector, we apply the function to each element.
logcons<-log(Consumption)str(logcons)
## num [1:4] 3.91 3.69 3.22 -Inf
Other functions take a vector as an input and return a single number as the output
meancons<-mean(Consumption)str(meancons)
## num 28.8
There are special values that numeric variables can take. These are Inf
and -Inf
for positive and negative infinity and NaN
for not a number. The presence of NaN
indicates an error.
log(-1)
## Warning in log(-1): NaNs produced
## [1] NaN
It is important to distinguish NaN
from NA
. The latter is used for missing data.
Another object common in R is known as a list. A list can contain completely different types of variables.
alist<-list(w=name, x=Drink, y=Consumption)
elements of lists are accessed using [[]]
or $
alist[[1]]
## [1] "Anastasios"
install.package
(do once).library
function (include at beginning of script).ggplot2
install.packages('ggplot2')
To load the package
library(ggplot2)
tidyverse
packagetidyverse
is a collection of packages.readr
is used for reading in data.dplyr
and tidyr
is used for manipulating data into an easy to use format.ggplot2
is used for visualisation.str(anscombe)
## 'data.frame': 11 obs. of 8 variables:## $ x1: num 10 8 13 9 11 14 6 4 12 7 ...## $ x2: num 10 8 13 9 11 14 6 4 12 7 ...## $ x3: num 10 8 13 9 11 14 6 4 12 7 ...## $ x4: num 8 8 8 8 8 8 8 19 8 8 ...## $ y1: num 8.04 6.95 7.58 8.81 8.33 ...## $ y2: num 9.14 8.14 8.74 8.77 9.26 8.1 6.13 3.1 9.13 7.26 ...## $ y3: num 7.46 6.77 12.74 7.11 7.81 ...## $ y4: num 6.58 5.76 7.71 8.84 8.47 7.04 5.25 12.5 5.56 7.91 ...
mean
function.xbar<-mean(anscombe$x4)ybar<-mean(anscombe$y4)str(xbar)
## num 9
str(ybar)
## num 7.5
var
function.vx<-var(anscombe$x4)vy<-var(anscombe$y4)str(vx)
## num 11
str(vy)
## num 4.12
cor
function.rxy<-cor(anscombe$x4,anscombe$y4)str(rxy)
## num 0.817
,
ggplot
function to create figures.ggplot2
package called qplot
.qplot
the variable(s) that you want to plot and the dataset.qplot
function tries to guess what type of plot you want.qplot(x4,data = anscombe)
qplot(x4,y4,data = anscombe)
qplot
is quite limited in what it is able to do.Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |