Skip to content
Course Notes: Intermediate R
  • AI Chat
  • Code
  • Report
  • Course Notes

    Use this workspace to take notes, store code snippets, and build your own interactive cheatsheet!

    # Import any packages you want to use here
    

    Take Notes

    Add notes here about the concepts you've learned and code cells with code you want to keep.

    Add your notes here

    lapply() - apply function over list or vector - output = list

    **sapply() ** - apply function over list or vector - try to simplify list to array

    vapply() - apply function over list of vector - explicity specify output format

    # sapply () and vapply ()
    cities <- c("New York", "Paris", "London", "Tokyo",
               "Rio de Janerio", "Cape Town")
    sapply(cities, nchar)
    
    ## vapply ## 
    ## vapply(X, FUN, FUN.VALUE, ... USE.NAMES = TRUE) ##
    vapply(cities, nchar, numeric(1))
    
    ## vapply() ## 
    ## first_and_last <- function(names) {
    ## 	name <- gsub("", "", name)
    ##	letters <- strsplit(name, split = "")[[1]]
    ##	return(c(first = min(letters), last=max(letters)))
    ##}
    
    sapply(cities, first_and_last)