#======================================================= # # Econ 21410: Examples of using *apply functions # # John Eric Humprhies 2014-04-17. # # abstract: this provides lists of using apply-type functions in R. # #======================================================== #============================== # Section 0: setup. #============================== rm(list=ls()) # Clear the workspace library(parallel) #============================== # Section 1: apply() #============================= rows= 1000 cols= 3000 mymatrix <- matrix(c(1:(rows*cols)), rows,cols) # take the mean of each row: #----------------------------- #loop tic <- proc.time() rowmeans <- c(rep(NA,rows)) for (i in 1:rows) { rowmeans[i] <- mean(mymatrix[i,]) } toc1 <- proc.time() - tic toc1 #apply tic <- proc.time() rowmeans <- apply(mymatrix,1,mean) toc2 <- proc.time() - tic toc2 # switch to columns (will be faster) tic <- proc.time() colmeans <- apply(mymatrix,2,mean) toc <- proc.time() - tic # defining functions inline: apply(mymatrix,2, function(j) min(j)/12 + rnorm(1) ) apply(mymatrix, 2, function(x) length(x[x%%3==0])) #============================== # Section 2: lapply() #============================= # applies a function to each element of a list: mylist<- list(a = 1:rows, b = 2:cols) lapply(mylist,sum) lapply(mylist,mean) lapply(1:10, function(i) 2^i) #============================== # Section 2: sapply() #============================= # applies a function to each element of a list-like object, returns a numeric object (a simplified list) mylist<- list(a = 1:rows, b = 2:cols) sapply(mylist,sum) sapply(mylist,mean) sapply(1:10, function(i) 2^i) sapply(1:100, function(x) c(x^2,2*x)) # a function with two arguemens sapply(1:10, function(x, y) mean(y[,x]), y=mymatrix) #============================== # Section 2: vapply() #============================= # Tell it what the output looks like to make it look better and sometimes go faster! fivenum(c(1:100)) l <- list(a = 1:10, b = 11:20) # fivenum of values using vapply l.fivenum <- vapply(l, fivenum, c(Min.=0, "1st Qu."=0, Median=0, "3rd Qu."=0, Max.=0)) #============================== # Section 2: mapply() #============================= # cool more complicated code. l1 <- list(a = c(1:10), b = c(11:20)) l2 <- list(c = c(21:30), d = c(31:40)) # sum the corresponding elements of l1 and l2 mapply(sum, l1$a, l1$b, l2$c, l2$d) mapply(max, l1$a, l1$b, l2$c, l2$d) mapply(rep, 1:4, 4:1) mapply(rep, times = 1:4, x = 4:1) #============================== # Section 2: #============================= # How do we add rows to columns? m <- matrix(c(1:10),10,10) m2 <- sapply(1:10,function(i) m[i,] + m[,i]) m <- matrix(c(1:5000),5000,5000) m3 <- matrix(NA,5000,5000) system.time( for (i in 1:5000) m3[i,] = m[i,] + m[,i] ) system.time( sapply(1:5000, function(i) m[i,] + m[,])) #============================== # Multicore also very easy! #============================== system.time(test <- lapply(1:3000000,function(x) sin(x)^2 + 3*x^3/x^(3.5)) ) system.time(test <- mclapply(1:3000000,function(x) sin(x)^2 + 3*x^3/x^(3.5),mc.cores = 8) ) system.time(test <- lapply(1:100,function(x) solve(matrix(rnorm(1000*1000),1000,1000) + diag(1,1000)) ) ) system.time(test <- mclapply(1:100,function(x) solve(matrix(rnorm(1000*1000),1000,1000) + diag(1,1000)) ,mc.cores = 4) ) unlist(test)