Skip to main content
Link
Menu
Expand
(external link)
Document
Search
Copy
Copied
Documentation
Document Digital Tools
R Language
Overview of R Material
R Syllabus
Background R Material
R Language Tips & Tricks
Douglas Bates Stat 692 Notes
R References
Curate R Data
Visualize Data with R
Network Observatins in Connected Graphs
Visualization & Graphics Links
Analyze Data
Systems Genetics
Profile & Simulate
Connect Data Tools Beyond `R`
Communication & Writing
Docker and Guix
Going Beyond R
LaTeX Writing Language
Linux Operating System & bash Shell
Non-Traditional Students: Professional MS and Certificates
Online & Blended Courses in Statistics
Professional Programs in Data Science or Data Analytics
Professional Skills for Data Science
R for teams in the data sciences
Reproducible Research Tools
Scaling up the split-apply-combine paradigm
Software Packages, Languages & Writing Tools
Stat 692 Fall 2014 Notes
Organize Data & Docs
R in VS Code with Radian
Python Language
Python Coding Strategy
Python Default Kernel for Quarto
Organize Projects with GitHub
Publish GitHub Pages
Embed Objects in GitHub Pages
Automate with GitHub Actions
Prepare Shinylive App Export
Publish Quarto Dashboard
Deploy Shinylive Apps
Version GitHub Docs
Environmental Systems
Geospatial Resources
Shiny Apps
AI (Artificial Intelligence)
AI Environments
Recovering Antigravity 1.x Conversations
Large Language Models (LLMs)
Team Science Platforms
Prompt Engineering
Prompt Examples
Version GitHub Docs
Assemble Powerpoint
Create Quarto Slides
Refactor Workflows
Configure Just-the-Docs
Check External Links
AGENTS.md — Documentation Repository
AGENTS_mini.md (Mini)
Migrate Documentation from DSHub
Context Engineering
Quarto Slides & References
Data Science
Big Data
Data Repositories
Documentation Source
Search Documentation
```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` The purpose of the base `apply` family of functions is to SIMPLIFY loops. ## loop approach ```{r} numSimulations <- 10000 ptm <- proc.time() results <- NULL for(iS in 1:numSimulations){ temp <- rnorm(100, 0, 1) results <- rbind(results, c(simNumber = iS, mean = mean(temp), min = min(temp), max = max(temp))) } (loopTime <- proc.time() - ptm) ``` ## apply approach - "apply" function ```{r} ptm <- proc.time() simulation <- function(ID){ temp <- rnorm(100, 0, 1) c(simNumber = ID, mean = mean(temp), min = min(temp), max = max(temp)) } results <- sapply(1:numSimulations, simulation) results <- t(results) (applyTime <- proc.time() - ptm) ``` But this is misleading; the bottleneck in the loop is that the array is being continuously copied and memory reallocated. If instead we allocate the memory initially, then the times become quite close. ```{r} ptm <- proc.time() results <- matrix(0, nrow = numSimulations, ncol = 4) for(iS in 1:numSimulations){ temp <- rnorm(100, 0, 1) results[iS,] <- c(simNumber = iS, mean = mean(temp), min = min(temp), max = max(temp)) } (loopTime2 <- proc.time() - ptm) ``` ### lapply and sapply `lapply` takes a vector or list (incl. data.frame) and returns list. `sapply` does the same but returns a simplified structure. ```{r} myList <- list(a = c(1,2,3,4,5), b = c(6,7,8,9,10), c = c(11,12,13,14,15)) fun_item1 <- function(data1, data2){ data1 } fun_type1 <- function(data1) { list(typeof(data1), data1) } lapply(myList, mean) sapply(myList, mean) myVector <- 0:10 lapply(myVector, `^`, 2) sapply(myVector, `^`, 2) myDF <- data.frame(names = c("ann","bob","corinne"), salary = c(15000, 25000, 150000), age = c(50, 40, 60)) lapply(myDF, mean) sapply(myDF, mean) ``` ### vapply `vapply` works on vectors, returns an array, requires FUN.VALUE ```{r} myStats <- function(data){ c(mean=mean(data), min=min(data), max=max(data)) } vapply(myList, myStats, FUN.VALUE = c(Avg = 0, minValue = 0, maxValue = 0)) ``` ### apply `apply` works on the margin of an array ```{r} myArray <- matrix(1:12, ncol=3) apply(myArray, 1, sum) apply(myArray, 2, sum) ``` ### tapply `tapply` uses factors instead of margins ```{r} myVector <- 1:12 myFactors <- rep(c("a","b"), c(5,7)) tapply(myVector, myFactors, min) ``` ### mapply `mapply` works where functions require more than one argument ```{r} mapply(`^`, 1:4, 2:5) ```