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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758824880, '216.73.216.5', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/R/functions/lapply(82): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78