sapply()
In this page:
sapply()
sapply(x, FUN) works like lapply() but tries to simplify the result into a vector or matrix when possible, instead of always returning a list. This makes it more convenient than lapply() for the common case where each call produces a single value. If simplification isn't possible, sapply() falls back to returning a list, just like lapply().
Note: sapply() is essentially "lapply() plus automatic simplification" -- reach for it first, and use lapply() when you specifically need a list.
Example: sapply()
numbers <- c(1, 2, 3, 4)
squared <- sapply(numbers, function(x) x^2)
print(squared)
cat(class(squared), "\n")
Login to try C/C++/Java/PHP code in the editor