R tutorials
Here you find our R tutorials. I will constantly add more videos in order to make an R hacker out of you.
# 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,"C:\Users\Jesper\Desktop\data.csv",row.names=F,sep=";") #Import the dataframe from the desktop read.csv("C:\Users\Jesper\Desktop\data.csv",header=T,sep=";",dec=",") #Import the dataframe using the set working directory command setwd("C:\Users\Jesper\Desktop") d<-read.csv("data.csv",header=T,sep=";",dec=",") d
#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("The price is to high","The service is great","I like the colors of the walls") #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]
#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("Length","Weight") 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("R",times=5) rep(c("R","Stats"),times=5) rep(c("R","Stats"),each=5) rep(c("R","Stats"),times=c(10,5)) tb<-table(rep(c("R","Stats"),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("R","Stats"),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
??"generalized additive model" install.packages("mgcv") library(mgcv)
#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 }
#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)
#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 }