Presentation is loading. Please wait.

Presentation is loading. Please wait.

R Course 5th lecture.

Similar presentations


Presentation on theme: "R Course 5th lecture."— Presentation transcript:

1 R Course 5th lecture

2 Task Create a function which removes even numbers from a vector
Hint: Use modulus and vector subsetting

3 Easiest solution is just
removeeven<- function(x){ x[x%%2 != 0] }

4 Can also use while and if
removeeven<-function(x){ i<-length(x) while(i>0){ if(x[i]%%2==0){ x<-x[-i] } i<-i-1 x

5 Can also use while and if
removeeven<-function(x){ i<-length(x) while(i>0){ if(x[i]%%2==0){ x<-x[-i] } i<-i-1 x

6 Could also use ceiling instead of modulus
removeeven<-function(x){ i<-length(x) while(i>0){ if(ceiling(x[i]/2)==x[i]/2){ x<-x[-i] } i<-i-1 x

7 Create a function printing the first 10 fibonacci numbers
Hint: will probably need to use subsetting and a for loop

8 Solution (can be done in several other ways)
fibonacci<-function(){ x<-1 y<-c(1) for(i in 1:9){ y[i+1]<-x x<-y[i+1]+y[i] } y

9 lapply() lapply() is a useful function in R, that allows you to apply a function that takes one input to a vector, and outputs a vector lapply(x,function,parameters) where x is a list/vector

10 Task Create a function that squares all odd numbers in a vector
Hint: create a nested function to square odd inputs and use lapply(x,function), which applies a function to a vector x

11 squareodd<-function(x){
ifodd<-function(y){ if(y%%2!=0){ y<-y^2 } y t(lapply(x,ifodd))

12 Task Create a function to find the number of ways of selecting k objects from n when the order of the selection does matter Create a function to find the number of ways when order doesn’t matter Hint: Use factorial() in R for factorials

13 Hint The formula for permutations is n!/(n-k)!
The formula for combinations is n!/k!(n-k)!

14 Plotting graphics Many functions to plot graphics
plot() - data represented differently depending on data types hist() - histogram barplot() - bar plot boxplot() - box plot pairs() - produces a matrix of scatterplots

15 Customising plots You can add arguments such as col="red",pch=4,main="title",xlab="xtitle",ylab="ytitle" See help(plot), help(“col"), or help(“pch") for more options or look online etc. Can use attach() to attach loaded datasets so can just use variable names E.g. attach(cars) allows you to use speed and dist, or can use cars$speed / cars$dist View(cars) shows you the dataset

16 pch point types

17 Example using cars dataset
attach(cars) plot(cars) or plot(speed,dist) plot(cars,col="red",pch=4) hist(speed)

18 Adding regression lines
You can create a linear model of a dataset using lm() This will give you the intercepts of the data You can then use abline(lm()) to add a fitted line to the plotted data You can also use fitted(lm()) to produce the fitted values of the corresponding x values

19 Regression using cars data
carsregr<-lm(dist~speed,data=cars) plot(cars,col="red",pch=4) abline(carsregr,col="blue")

20 Boxplots and pairs attach(mtcars)
boxplot(disp~cyl) plots a boxplot of disp given cyl pairs(~mpg+disp+drat+wt,data=mtcars,main="scatterplot matrix") Can also colour by category plot(mpg,disp,col=as.numeric(as.factor(disp)),pch=4)

21 Task Use the default dataset mtcars, and create a scatterplot cylinders versus miles per gallon, labelling the axes and graph and adding titles, make it look more aesthetically pleasing and easy to read (choose clear point styles and colours), and plot a line of best fit

22 Create a histogram of the default dataset AirPassengers
You can customise drawing using limits, breaks etc. (use help for options) Can also add density function with lines(density(AirPassengers))

23 Use state. x77 dataset to plot population vs area and to calculate pop
Use state.x77 dataset to plot population vs area and to calculate pop. density Also compare life exp. Vs. murder rates (plot points, create a linear model, plot fitted line etc.) Compare illiteracy vs. HS grad rates


Download ppt "R Course 5th lecture."

Similar presentations


Ads by Google