Control Structures Hara URL:
Contents 1 Conditional execution 1.1 Selecting subsets of vector 2 Loops 2.1 Implicit loops 2.2 Explicit loops
1.Conditional execution Define condition or situation when you want to activate the script which you want to execute. The most simple way is to use “if-else” function. How to do it? If the condition is TRUE, R activates this script. If the condition is FALSE, R activates this script. Specify the condition here ( in the first brackets).
Available parameters (symbols) for the condition x == y "x is equal to y" x != y "x is not equal to y“ ( ※ exclamation mark plus equal) x > y "x is greater than y" x < y "x is less than y" x <= y "x is less than or equal to y" x >= y "x is greater than or equal to y“ You may want to combine these above conditions using & or && for AND | or || for OR.
#example 1 x <- 2 # x gets the value 2 if(x==3){ print("This is true") } else { print("This is false") } #example 2 y <- 4 # y gets the value 4 if(x==2 && y>2){ print("x equals 2 and y is greater than 2") } else { print ("FALSE") } #example 3 if(x==2 && y<2){ print("x equals 2 and y is greater than 2") } Example First, R judge if the condition is TRUE or FALSE. Then, execute the script.
To simply activate this conditional execution, you can also use “ifelse” function !! Explanation) ifelse(condition, TRUE, FALSE) # first argument is the condition # second argument is the treatment if the condition is true # and third argument is the treatment if the condition is false #example 4 x <- 1:10 x ifelse(x 8, x, 0)
1-1. Selecting subsets of vector #example 5 # Let’s make a vector x<- runif(10) # runif(n) means a n random samplings from normal distribution between 0 to 1. x #check the vector, x. x<0.5 #judge if the x is less than 0.5 or not (R returns TRUE or FALSE) x[x<0.5] #We can ask R, whether x is less than 0.5 or not. #specify a subset #First, R judges the condition (TRUE or FALSE). R returns a vector of numbers when the x is less than 0.5 (condition is TRUE).
1-1. Selecting subsets of vector #example 6 # You can specify the inclusion or exclusion place of vector using [ ], or [- ]. x<- 1:10 # assign x a vector from 1 to 10. x #check x x[5] #specify inclusion places x[c(2,4,6,8,10)] #specify inclusion places x[-1:-5] #specify exclusion places
2. Loops Loop is a set of operations in a computer program that are continuously repeated. With loop function, R execute a script again and again and again … continuously. Two ways to do loop in R ① Use built-in function, “apply” family. → 2-1. Implicit loops ② Write loops using “for”, “repeat” and “while”. → 2-2. Explicit loops
2.1 Implicit loops Explanation ) apply ( x, 1 or 2, function) # first argument is matrix (or array) # second argument is 1 or 2. 1 indicates rows, and 2 indicates colums over which the function will be applied # third argument is function #example 7 # Let’s use “apply” function # make a matrix (anything is OK) → This is NOT a today’s main topic # just understand that’s just a way it is! N <- 10 x1 <- rnorm(N, mean = 0, sd = 1) x2 <- rnorm(N, mean = 0, sd = 1) + x1 + 1 male <- rbinom(N,1, 0.48) y <- 1 + x1 + x2 + male + rnorm(N) mydata <- data.frame(y,x1,x2,male)
#example 7 (continue) #check mydata mydata # script for calculation of mean in each colum apply (mydata, 2, mean) There are several “apply family” functions… “lapply” applies a function to each column and returns results as a list. “sapply” is similar but the output is simplified. It may be a vector or a matrix “tapply” applies the function for each level of a factor and returns results as a table #example 8 lapply (mydata, mean) sapply (mydata, mean) tapply (mydata$y, mydata$male, mean)
2.2 Explicit loops explanation) for (i in vector) { statement } repeat { statement if (condition) breaks } # if there is no breaks, statement will repeat forever. while (condition) { statement } There are 3 ways to write loops : “for”, “repeat” and “while”.
#example 9 for (i in 1:5) { print(i) } #example 10 repeat { g<-rnorm(1) if(g>1.0) break cat(g, "\n") } # “\n” means line break # cat outputs the object #example 11 g<-0 while (g<1) { g<-rnorm(1) cat (g, "\n") }