## R tutorials

Here you find our R tuto­ri­als. I will con­stant­ly add more videos in order to make an R hack­er out of you.

## #4 Scripts, save and load csv

#### SCRIPT

```
# Create vectors and dataframe

x<-1:10
y<-11:20
d<-data.frame(Length=x,Weight=y)
d

#Save dataframe on desktop

write.table(d,&quot;C:\Users\Jesper\Desktop\data.csv&quot;,row.names=F,sep=&quot;;&quot;)

#Import the dataframe from the desktop

#Import the dataframe using the set working directory command

setwd(&quot;C:\Users\Jesper\Desktop&quot;)
d
```

## #5 Lists

#### SCRIPT

```#Create vectors and dataframe for satisfaction index

General<-sample(1:10,10,replace=TRUE)
Expectations<-sample(1:10,10,replace=TRUE)
Ideal<-sample(1:10,10,replace=TRUE)

d<-data.frame(General=General,Expectation=Expectations,Ideal=Ideal)

#Create vector and dataframe for loyalty index

recommend<-sample(1:10,10,replace=TRUE)

dr<-data.frame(Recommend=recommend)

#Create vectors and dataframe for value index

money<-sample(1:10,10,replace=TRUE)
others<-sample(1:10,10,replace=TRUE)

dv<-data.frame(Money=money,Others=others)

#Create text vector
text<-c(&quot;The price is to high&quot;,&quot;The service is great&quot;,&quot;I like the colors of the walls&quot;)

#Create list

l<-list(d,dr,dv,text)

#Subset the first element

l[[1]]

#Subset the first vector of the first element

l[[1]][1]

#Subset rows 2 to 5 of the first vector of the first element

l[[1]][[1]][2:5]
```

## #6 Functions

#### SCRIPT

```#Common functions

c(1,2,3,10,34)

cbind(1:10,11:20)

data.frame(cbind(1:10,11:20))

d<-data.frame(cbind(1:10,11:20))

colnames(d)<-c(&quot;Length&quot;,&quot;Weight&quot;)

d

ls<-list(

c(1,2,3,10,34),
cbind(1:10,11:20),
data.frame(cbind(1:10,11:20))

)

ls

seq(from=1,to=10,by=1)

seq(from=1,to=10,by=3)

seq(from=1,to=10,by=0.5)

rep(11,times=10)

rep(1:5,times=10)

rep(1:5,each=10)

rep(1:5,times=c(2,5,7,1,4))

rep(&quot;R&quot;,times=5)

rep(c(&quot;R&quot;,&quot;Stats&quot;),times=5)

rep(c(&quot;R&quot;,&quot;Stats&quot;),each=5)

rep(c(&quot;R&quot;,&quot;Stats&quot;),times=c(10,5))

tb<-table(rep(c(&quot;R&quot;,&quot;Stats&quot;),times=c(10,5)))

tb

prop.table(tb)

prop.table(tb*100)

round(prop.table(tb*100),digits=2)

length(1:10)

length(d)

length(ls)

class(rep(c(&quot;R&quot;,&quot;Stats&quot;),each=5))

#Basic statistical functions

mean(1:10)

mean(c(1:10,NA))

mean(c(1:10,NA),na.rm=TRUE)

sd(1:10)

sd(c(1:10,NA))

sd(c(1:10,NA),na.rm=TRUE)

var(1:10)

min(1:10)

max(1:10)

#Searching for functions

?mean

??mean

```

## #7 R packages

#### SCRIPT

```??&quot;generalized additive model&quot;
install.packages(&quot;mgcv&quot;)
library(mgcv)
```

## #8 Loops part 1 — the for loop

#### SCRIPT

```#The for loop

#Example 1 - calculate log of x using a for loop

x<-sample(1:30,40,replace=TRUE)

log.x<-NULL

for(i in 1:length(x)){
log.x[i]<-log(x[i])
}

#Example 2 - Calculate mean of elements in a list

y<-sample(50:90,20,replace=TRUE)
z<-sample(20:55,30,replace=TRUE)

l<-list(x,y,z)

mean.l<-list(NULL)

for(i in 1:length(l)){
mean.l[[i]]<-mean(l[[i]])
}

unlist(mean.l)

#Example 3 - Calculate mean of columns in a dataframe

df<-data.frame(x=sample(20:70,20,replace=TRUE),
y=sample(80:120,20,replace=TRUE))

mean.df.col<-NULL

for (i in 1:length(df)){

mean.df.col[i]<-mean(df[,i])
}

#Example 4 - loop over time steps

m<-matrix(ncol=11,nrow=10)
m[,1]<-sample(200:450,10,replace=TRUE)

for(i in 2:11){
m[,i]<-m[,i-1]*1.2

}

```

## #9 Loops part 2 — apply & lapply

#### SCRIPT

```#Apply and lapply

#Example 1 - calculate log of x using apply

x<-sample(1:30,40,replace=TRUE)
log.x<-apply(data.frame(x),2,log)

#Example 2 - Calculate mean of elements in a list using lapply

y<-sample(50:90,20,replace=TRUE)
z<-sample(20:55,30,replace=TRUE)

l<-list(x,y,z)

mean.l<-unlist(lapply(l,mean))

#Example 3 - Calculate mean of columns in a dataframe using apply

df<-data.frame(x=sample(20:70,20,replace=TRUE),
y=sample(80:120,20,replace=TRUE))

mean.col<-apply(df,2,mean)
mean.row<-apply(df,1,mean)
```

## Generate the fibonacci sequence

#### SCRIPT

```#Recursion
fib<-function(n,f=NULL){
if(n==0) return(0)
if(is.null(f)) f<-c(0,1)

if(length(f) == n+1){
return(f)
}else{
l<-length(f)
x<-f[l]+f[l-1]
f<-c(f,x)
return(fib(n,f))
}
}

#While loop

fibWhile<-function(n){
if(n==0) return(0)
f<-c(0,1) while(n+1>length(f)){
l<-length(f)
x<-f[l]+f[l-1]
f<-c(f,x)
}
f
}
```