How to use sapply() in R? Explain with an example

687    Asked by LucasBROWN in Data Science , Asked on Jan 7, 2020
Answered by Lucas BROWN

Sapply() takes list as an input but returns an output as vectors.Let us explain with an example

x <- list(a = 1, b = 1:3, c = 10:100)

   #Compare with above; a named vector, not a list

   sapply(x, FUN = length)

   a b c

   1 3 91

   sapply(x, FUN = sum)

   a b c

   1 6 5005



Your Answer

Interviews

Parent Categories