Why do sapply() and lapply() have same result?

Jaehee Hur

I am new to R. I was practicing lapply() and sapply() function: lapply() returns list, and sapply() returns as vector/matrix. Now there I don't understand why do lapply() and sapply() have same result as a list?

> x1 <- lapply(list(1:3, 25:29), function(x) {2*x})
> x2 <- sapply(list(1:3, 25:29), function(x) {2*x})
> x1
[[1]]
[1] 2 4 6

[[2]]
[1] 50 52 54 56 58

> x2
[[1]]
[1] 2 4 6

[[2]]
[1] 50 52 54 56 58

> str(x2)
List of 2
 $ : num [1:3] 2 4 6
 $ : num [1:5] 50 52 54 56 58
> mode(x2)
[1] "list"

After, I checked unlist() and it return same input as a vector. It's hard to understand why do sapply() return list type as a result.

Thank you.

akrun

If the length of the list elements are not the same, sapply will not coerce it to matrix and will have the same result as lapply

We can check the Usage of ?sapply and ?lapply

lapply(X, FUN, ...)

sapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)

The simplify=TRUE is the one that converts a list of equal length output to a matrix

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

why sapply() and tapply() have the same result, but not identical?

From Dev

Why is `unlist(lapply)` faster than `sapply`?

From Dev

why do flexbox children have the same height?

From Dev

Why do these two arrays have the same shape?

From Dev

create a function providing the same result as lapply in R

From Dev

Why do these two apis for the same site not have the same methods?

From Dev

Why do different methods of same object have the same `id`?

From Dev

Why do different partitions on the same disk not have the same mount point?

From Dev

Why do these two apis for the same site not have the same methods?

From Dev

sapply and lapply for changing integer to factor

From Dev

Why do I repeatedly get the same result with this unsynchronized code?

From Dev

Why do a redirect, and an A record result in different media queries on the same device

From Dev

find test and bash test do not give the same result, why?

From Dev

Why does Ruby have zip and transpose when they do the same thing?

From Dev

Why do two new objects not have the same hash code?

From Dev

Why do all methods have the same name in delegate?

From Dev

Why do count(1), count(column) and count(*) have the same cost?

From Dev

Why do these 2 queries have the same "GB processed" (and thus cost)?

From Dev

Why do the objects created in a loop have the same address?

From Dev

Why do new objects in multiprocessing have the same id?

From Dev

Why do almost all Java binaries have the same size

From Dev

Why do all computers on my network have the same MAC address?

From Dev

Why do multiple instances of Mate-terminal have the same PID?

From Dev

Why do "--i" and "i--" have the same behavior in a Java for loop?

From Dev

Why do these 2 queries have the same "GB processed" (and thus cost)?

From Dev

Why do I have the same package installed for both architectures?

From Dev

Why do .ftl file are exactly same have different file size?

From Dev

Scala: why mutable Map and immutable Map have different result on same custom class instance as key?

From Dev

Why does &v[1] + &v[2] have the same result as v[1] + v[2] in Rust?

Related Related

  1. 1

    why sapply() and tapply() have the same result, but not identical?

  2. 2

    Why is `unlist(lapply)` faster than `sapply`?

  3. 3

    why do flexbox children have the same height?

  4. 4

    Why do these two arrays have the same shape?

  5. 5

    create a function providing the same result as lapply in R

  6. 6

    Why do these two apis for the same site not have the same methods?

  7. 7

    Why do different methods of same object have the same `id`?

  8. 8

    Why do different partitions on the same disk not have the same mount point?

  9. 9

    Why do these two apis for the same site not have the same methods?

  10. 10

    sapply and lapply for changing integer to factor

  11. 11

    Why do I repeatedly get the same result with this unsynchronized code?

  12. 12

    Why do a redirect, and an A record result in different media queries on the same device

  13. 13

    find test and bash test do not give the same result, why?

  14. 14

    Why does Ruby have zip and transpose when they do the same thing?

  15. 15

    Why do two new objects not have the same hash code?

  16. 16

    Why do all methods have the same name in delegate?

  17. 17

    Why do count(1), count(column) and count(*) have the same cost?

  18. 18

    Why do these 2 queries have the same "GB processed" (and thus cost)?

  19. 19

    Why do the objects created in a loop have the same address?

  20. 20

    Why do new objects in multiprocessing have the same id?

  21. 21

    Why do almost all Java binaries have the same size

  22. 22

    Why do all computers on my network have the same MAC address?

  23. 23

    Why do multiple instances of Mate-terminal have the same PID?

  24. 24

    Why do "--i" and "i--" have the same behavior in a Java for loop?

  25. 25

    Why do these 2 queries have the same "GB processed" (and thus cost)?

  26. 26

    Why do I have the same package installed for both architectures?

  27. 27

    Why do .ftl file are exactly same have different file size?

  28. 28

    Scala: why mutable Map and immutable Map have different result on same custom class instance as key?

  29. 29

    Why does &v[1] + &v[2] have the same result as v[1] + v[2] in Rust?

HotTag

Archive