Download presentation
Presentation is loading. Please wait.
Published bySage Banford Modified over 9 years ago
1
How I work Write code Notepad (or similar), and copy and paste to R Read data always to object dat
2
Terminology Function – A collection of code that does some data manipulation/calculation etc. – Example: calculate a mean; mean() Argument – Specifies or modifies what a function does – Written between brackets – Calculate a mean of vector a: mean(a) – Calculate a trimmed mean of vector a: mean(a, trim=0.5)
3
Terminology Object – Typically a data object hold in R memory – data frame, matrix, vector, factor, list,... – Also functions are objects Attribute – Specifies the object somehow – For example, number of rows and columns in a data frame are its dimensional attributes (there could be others, also)
4
Saving into objects or assignment Give value of 1 to the vector called a: – a<-c(1) – a<-1 – a=1 All of them work correctly, but only the first is actually syntactically correct The result is that the a gets the value of 1
5
Brackets Functions / commands: always () – mean() Data flow: either ( ) or { } – (1+2)^2 – {1+2}^2 – if(x) { } – function(x) { } Subsetting: always [] – d[1,2]
6
Subsetting a<-c(1,2,3) a[1] # no comma! b<-c(4,5,6) d<-data.frame(a,b) d[1,1] # comma! – Tables are indexes in the order of rows and columns – Inside square brackets, you'll list the rows before the comma, and columns after the comma For data frames – a[,1] # first columns; is equal to – a$V1 # after the dollar sign, the name of the column
7
Clear memory / delete objects a<-1 ls() Aha, there's an object a in memory, and I want to delete it – rm(a) – rm(list=ls())
8
Functions Functions perform some specific action – read a file (read.table) – create a data frame (data.frame) – calculate a mean (mean) – etc. Often used as building block of larger programs. You can make your own, for instance, calculate a body mass index.
9
Functions Example: – a<-1:10 Every time you need to calculate a mean, you can type (and case by case, change the both a's to denote the right data): – sum(a)/length(a) If you have a function, you write it once, and use everywhere you like.
10
Functions m<-matrix(ncol=2000, nrow=100, data=runif(2000*100)) Calculate a mean for every column: – sum(m[,1])/length(m[,1]) – sum(m[,2])/length(m[,2]) – sum(m[,3])/length(m[,3]) Or create a function and use it: s<-function(x) { sum(x)/length(x) } s(m[,1])
11
Loops Calculate a sum of all columns of the matrix m, without having to type the function calls repeatedly For every column of the matrix (from 1 to 2000), calculate the sum of the values of the column: for(i in 1:2000) { s(m[,i]) }
12
Loops For every column of the matrix (from 1 to 2000), repeat the calculation of the sum of the values in the table, and store the results into an object res: res<-rep(NA, 2000) for(i in 1:2000) { res[i]<-s(m[,i]) }
13
Loops For every column of the matrix (from 1 to 2000), repeat the calculation of the sum of the values in the table, and store the results into an object res: for(i in 1:2000) { write(s(m[,i]), file=”test.txt”, append=T) }
14
Loops For every column of the matrix (from 1 to 2000), repeat the calculation of the sum of the values in the table, and store the results into an object res: res<-rep(NA, 2000) i<-1 while(i<=2000) { res[i]<-s(m[,i]) i<-i+1 }
15
For- and while-loops For-loops: – Use when you know the number rounds of the loop While-loops: – Use when you don't know the number of rounds – But you need to know some criteria for when to stop
16
Loops When to use a loop? – When you want to repeat the same steps for several things – You can it by hand, but using a loop is easier
17
Draw > 2000 images one by one? I don't think so. We used a loop instead. If you have less than a few dozen things to write repeatedly by hand, it might be easier to not use a loop
18
An example Aim: one plot per column, save into pdf files – m<-matrix(ncol=2000, nrow=100, data=runif(2000*100)) – plot(x=1:100, y=m[,1], pch=16) – # repeat the previous for each column # Automates the previous thing – for(i in 1:ncol(m)) { – pdf(paste0("plot", i, ".pdf")) – plot(x=1:100, y=m[,1], pch=16) – dev.off() – }
19
An example Aim: one plot per column, save into pdf files – m<-matrix(ncol=2000, nrow=100, data=runif(2000*100)) – plot(x=1:100, y=m[,1], pch=16, type="l") – # repeat the previous for each column # Automates the previous thing – for(i in 1:ncol(m)) { – pdf(paste0("plot", i, ".pdf")) – plot(x=1:100, y=m[,1], pch=16, type="l") – dev.off() – }
20
Loops Always, e.g.: for(i in from:to) { } What you want to do in a loop, be it what ever, goes between the curly brackets
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.