Download presentation

Presentation is loading. Please wait.

Published byTheresa Simon Modified over 2 years ago

1
1 R Programming Zhi Wei

2
2 Language layout Three types of statement expression: it is evaluated, printed, and the value is lost (3+5) assignment: passes the value to a variable but the result is not printed automatically (out<- 3+5) comment: (#This is a comment)

3
3 Loops and conditionals Conditional if (expr) expr if (expr) expr else expr Iteration repeat expr while (expr) expr for (name in expr1) expr For comparisons use: equal: == not equal: != greater/less than: > < greater/less than or equal: >= <= NOT, AND, OR: !, &, |

4
4 Examples What does the following code do? if (value==1) {check<-1} else {check<-0} counter<-0 for (i in 1:10) { if (geneExpr[i,2]<10){counter<-counter+1} } counter=sum(geneExpr[c(1:10),2]<10)

5
Accessing a data frame with logical vector data set ewr (UsingR package) >attach(ewr) >boxplot(ewr[inorout=="in", 3:10], main="Taxi in") >detach(ewr) subset() function new.df = subset(old.df, subset=…, select=…) ewr.in = subset(ewr, subset= inorout=="in", select=3:10) ewr.out = subset(ewr, subset= inorout=="out", select=3:10) 5

6
Sorting a data frame by one of its column >attach(mtcars) >mtcars[order(mpg),] >mtcars[order(mpg, decreasing=T),] >mtcars[order(cyl,hp),] 6

7
Vectorization: avoid Loop (page 26, supp. R book, moodle has a copy) apply(X, MARGIN, FUN,...) find minimum values of each row of matrix m m <- matrix(rnorm(100), ncol=4) apply(m, 2, min) ##- function with extra args: myMin cut]) } apply(m,2, myMin, cut=0) lapply(X, FUN,...), sapply, tapply lapply(thuesen, mean, na.rm=T) sapply(thuesen, mean, na.rm=T) tapply(energy$expend, energy$stature, median) aggregate(energy$expend, energy["stature"], median) 7

8
Edit and Save your script Can write many lines of code in any of your favorite text editors and run all at once Simply paste the commands into R Save your script and then Use function source( “ path/yourscript ” ), to run in batch mode the functions/codes saved in file “ yourscript ” (use options(echo=T) to have the commands echoed) 8

Similar presentations

OK

What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c)1 1 1 1 1 1 a) 1 2 3 1 1 1 d)1 2 3 1 1 1 b) 1 2 3 1 2 3.

What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c)1 1 1 1 1 1 a) 1 2 3 1 1 1 d)1 2 3 1 1 1 b) 1 2 3 1 2 3.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Viewing ppt on ipad 2 Ppt on opera web browser Ppt on operating system concepts Ppt on world environment day posters Download ppt on p block elements class 11 Ppt on the road not taken by frost Animated ppt on magnetism and electromagnetism Ppt on 3d figures Ppt on conservation of momentum lab Stem and leaf display ppt on tv