###### ## Abbrivations len <- length f <- `function` #### DOESN'T WORK ###### ## BASIC ## If a <- 3 if(a==3) print("is 3") else print("not 3") ## For for(i in 1:10) print(i) ##### ## Errors ## Stop on error stop ("error happend") ## null? is.null(NULL) ##### ## Data Structures ## Vector num3to5 <- c(3,4,5) numTo10 <- 1:10 ## Matrix ones <- rep(1,times=9) m1 <- matrix(ones, nrow=3, ncol=3, byrow = FALSE) ## number of rows nrow(m1) ## List of matrices matrices <- list(m1, m1, m1) firstm <- matrices[[1]] ##### ## MISC ## sum, max, sum(1:10) max(1:10) ## String paste("a", "b") cat(c(1,2)) #### ## MATH MISC ## transpose matrix t(m1) ## random sample sample(1:10, 1) ## Apply ## List (of matrices) ###### ## HIGH LEVEL FUNCTIONS ## apply apply(statex77, 2, median) # 2:bycolumn 1:byrow ## apply2 m <- matrix(c(1:10, 11:20), nrow = 10, ncol = 2) # divide all values by 2 apply(m, 1:2, function(x) x/2) ## which which(1:10 == 3) ## Filter gr3 <- function(x) if(x>3) return(TRUE) else return(FALSE) num4to10 <- Filter(gr3, 1:10) ## Map nums <- unlist( Map(sqrt, 1:10) ) ## Map - Reduce nums_string <- Reduce(paste, Map(sqrt, 1:10) ) ## mapply ## sapply (simple) sapply(3:5, function(i)i+1) ## lapply (list) ## reading numbers ## further apply stuff: ### http://nsaunders.wordpress.com/2010/08/20/a-brief-introduction-to-apply-in-r/