a
and the number 2 in a variable b
?a<-1b=2
<-
or =
to assign variables. To see what is stored in a variable
print(b)
## [1] 2
str(b)
## num 2
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())
rm(list=ls())
getwd()
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 ^
.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
Many if not most of the mistakes you make in R occur because you enter the incorrect type of input in a function.
?
?sqrt
??
. ?
?sqrt
??
. ??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
In stats we have many observations for each variable. The function c()
stores these in a vector. Suppose we have the following data:
Names | Drink | Consumption | Satisfaction |
---|---|---|---|
Andrew | Coke | 50 | 5 |
Boris | Pepsi | 40 | 4 |
Cathy | Coke | 25 | 4 |
Diana | 7Up | 0 | 3 |
First let's create the variable Consumption
Consumption<-c(50,40,25,0)print(Consumption)
## [1] 50 40 25 0
str(Consumption)
## num [1:4] 50 40 25 0
Put values of drink into a variable.
The solution is
Drink<-c('Coke','Pepsi','Coke','7Up')print(Drink)
## [1] "Coke" "Pepsi" "Coke" "7Up"
str(Drink)
## chr [1:4] "Coke" "Pepsi" "Coke" "7Up"
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
The values Inf
and -Inf
refer to positive and negative infinity. The value NaN
stands for not a number and 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.
.rds
extension it is easyBeer<-readRDS("Beer.rds")
str(Beer$alcohol)
## num [1:35] 3.7 4.1 4.2 4.3 2.9 2.3 4.2 4.7 5.5 4.7 ...
Another object common in R is known as a list. A list can contain completely different variables.
alist<-list(w=name, x=Drink, y=Beer)
elements of lists are accessed using [[]]
or $
alist[[1]]
## [1] "Anastasios"
install.package
.library
functioninstall.package
.library
functioninstall.package
.library
functionggplot2
install.packages('ggplot2')
To load the package
library(ggplot2)
ggplot2
, however if you have learnt base graphics in another unit and prefer this, then you can use it.ggplot2
, however if you have learnt base graphics in another unit and prefer this, then you can use it.ggplot2
, including some that are free online.To demonstrate ggplot2
we use the dataset mpg
, which contains information on the fuel efficiency of different cars. This can be loaded into R using the command
data(mpg)
It is data that comes together with ggplot2
.
ggplot(mpg,aes(x=cty,y=hwy))
geom_point()
.ggplot(mpg,aes(x=cty,y=hwy))+geom_point()
We can think of colour as a third aesthetic
ggplot(mpg,aes(x=cty,y=hwy,col=cyl))+geom_point()
ggplot(mpg,aes(x=cty,y=hwy,col=drv))+geom_point()
A useful function in ggplot2
is to use qplot
which will try to guess the plot you want. Try these examples
qplot(x=cty,y=hwy,data=mpg)qplot(x=cty,data=mpg)
pdf('myplot.pdf')qplot(x=cty,data=mpg)dev.off()
echo=TRUE
to present the code or echo=FALSE
to hide it.filter
function.filter
function.?Logic
) and relational operators (try ?Comparison
).filter
function.?Logic
) and relational operators (try ?Comparison
).To create a new data frame that only includes 4 wheel drives
library(dplyr)mpg_4wd<-filter(mpg,drv=='4')
To exclude all 4 wheel drives
mpg_no4wd<-filter(mpg,drv!='4')
Suppose we only want to consider cars that are 4 wheel drives and can drive more than 15 miles per gallon on the highway
mpg_4wd_hwyg15<-filter(mpg,(drv=='4')&(hwy>15))
Or those that are either 4 wheel drives or can drive less than 15 miles per gallon in the city
mpg_4wd_ctyl15<-filter(mpg,(drv=='4')|(cty<15))
dplyr
but is more verbose. mpg_4wd_ctyl15<-mpg[((mpg$drv=='4')|(mpg$cty<15)),]
mpg_4wd_ctyl15<-filter(mpg,(drv=='4')|(cty<15))mean_sd_hwy<-summarise(mpg_4wd_ctyl15,mean(hwy),sd(hwy))mean_sd_hwy
## # A tibble: 1 x 2## `mean(hwy)` `sd(hwy)`## <dbl> <dbl>## 1 19 3.91
filter(mpg,(drv=='4')|(cty<15))%>% summarise(mean(hwy),sd(hwy))%>% print
## # A tibble: 1 x 2## `mean(hwy)` `sd(hwy)`## <dbl> <dbl>## 1 19 3.91
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 |