##################################### ########### R Democode Day 6################### ##################################### ##################################### # Loops ##################################### # Calculate the first twelve elements of the Fibonacci series fib <- c(1,1) for (i in 1:10) fib <- c(fib, fib[i]+fib[i+1]) fib # [1] 1 1 2 3 5 8 13 21 34 55 89 144 ##################################### # Other loops: while, repeat, break ##################################### # if -- else # Conditional execution: if ( fib[i+2] > 1E10 ) break if ( fib[i+2] %% 3 == 0) print(paste('hit', fib[i+2])) else print(fib[i+2] %% 3) # Select elements from 2 vectors based on condition ifelse(c(TRUE,FALSE,TRUE), 1:3, 11:13) # [1] 1 12 3 ##################################### # Apply ##################################### d.sport <- read.table( "http://stat.ethz.ch/Teaching/Datasets/NDK/sport.dat", header=TRUE) apply(d.sport, 2, mean) ## weit kugel hoch disc stab speer punkte ## 7.6 15.2 202.0 46.4 498.0 62.0 8444.7 apply(d.sport, 2, mean, trim=0.3) # trimmed mean ## weit kugel hoch disc stab speer punkte ## 7.59 15.19 201.86 46.42 495.71 63.00 8397.86 # Apply a function to each component of a list t.kugel <- d.sport$kugel t.l <- hist(t.kugel,plot=FALSE) # see lists str(t.l) lapply(t.l[1:2], length) ## $breaks ## [1] 8 ## $counts ## [1] 7 sapply(t.l[1:4], length) ## breaks counts intensities density ## 8 7 7 7 ##################################### # Aggregate ##################################### data(sleep) str(sleep) head(sleep) # Summaries over groups of data aggregate(sleep[,'extra'], list(sleep[,'group']), median) ## Group.1 x ## 1 1 0.35 ## 2 2 1.75 # Result is a data.frame # Similar: by, tapply ################################### # Write your own Function ################################### # Get the maximal value of a vector and its index f.maxi <- function(vec) { t.max <- max(vec, na.rm=TRUE) t.i <- match(t.max, vec) c(max=t.max, i=t.i) } f.maxi(c(3,4,78,2)) ## max i ## 78 3 # Note: R provides the function which.max # This function can now be used in apply apply(d.sport, 2, f.maxi) ## weit kugel hoch disc stab speer punkte ## max 8.07 16.97 213 49.84 540 70.16 8824 ## i 2.00 13.00 8 4.00 6 3.00 1 ########################### # Error Handling ########################### # Show the ``stack'' of function calls: traceback() # If you write your own functions: ?debug browser() # options(error=recover) calls browser() when an error occurs. ########################### ####### Options ### ########################### options(digits=3) # rounds results to 3 significant digits (for printing) options("digits") # Note: The setting of options (and par) will be lost upon q() ?Startup # Linux: provide a file called .Rprofile ########################### ##### Packages ######### ########################### # loading additional ``packages''. library(MASS) require(MASS) # How to find a command and the corresponding package? help.search("...") ## slow! # On the internet # http://www.r-project.org/search.html (or Google!) # What does a package do? library(help=MASS)