Search notes:

R function: lapply

lapply applies a function to each element of a vector.

Simple example

In the following example, the vector is x and the function is func. lapply thus creates a new vector that subsequently is plotted for verification:
func <- function(x) {
  if (x < 0.25) {
    return (1-4*x)
  }
  if (x < 0.50) {
    return (-1 + 4*x)
  }
  if (x < 0.75) {
    return (3 - 4*x)
  }
  return (-3 + 4*x)
}


x <- 0:20/20
y <- lapply(x, func)

X11()

plot(x, y)
lines(x,y, col='red')

locator(1)
Github repository about-r, path: /functions/lapply.R

Print each element of a vector on its own line

> vec <- c('foo', 'bar', 'baz')
> nil <- lapply(vec, cat, '\n')

See also

Other apply functions
The foreach package
This shiny example uses lapply to produce a HTML unordered list of HTML tags.
Index to (some) R functions

Index