Difference between lapply() and sapply() functions
lapply() function displays the output as a list whereas sapply() function displays the output as a vector. lapply() and sapply() functions are used to perform some operations in a list of objects. sapply() function in R is more efficient than lapply() in the output returned because sapply() stores values directly into a vector.
Example 1: The lapply() function returns the output as a list whereas sapply() function returns the output as a vector
R
print ( "Operations using lapply() function: " ) # Initializing list1 # list1 have three objects a, b, and c # and they all are numeric objects (same data type) list1 <- list (a = 1: 20, b = 25:30, c = 40:60) # Printing the length of list1 objects lapply (list1, length) # Printing the sum of elements present in the # list1 objects lapply (list1, sum) # Printing the mean of elements present in the # list1 objects lapply (list1, mean) # Printing the cumulative sum of elements # present in the list1 objects lapply (list1, cumsum) print ( "Operations using sapply() function: " ) # Initializing list2 # list2 have three objects a, b, and c # and they all are numeric objects (same data # type) list2 <- list (a = 1: 20, b = 25:30, c = 40:60) # Printing the length of list2 objects sapply (list2, length) # Printing the sum of elements # present in the list2 objects sapply (list2, sum) # Printing the mean of elements # present in the list2 objects sapply (list2, mean) # Printing the cumulative sum # of elements present in the list2 objects sapply (list2, cumsum) |
Output:
Example 2: As you can see in the output, The lapply() function returns the output as a list whereas sapply() function returns the output as a vector.
R
print ( "Operations using lapply() function: " ) # Initializing list1 # list1 have three objects a, b, and c # and they all are numeric objects (same data type) list1 <- list (a=11: 12, sample ( c (1, 2, 5, 3), size=4, replace= FALSE ), c=40: 60) # Printing the length of list1 objects lapply (list1, length) # Printing the sum of elements present in the # list1 objects lapply (list1, sum) # Printing the mean of elements present in the # list1 objects lapply (list1, mean) # Printing the cumulative sum of elements present # in the list1 objects lapply (list1, cumsum) print ( "Operations using sapply() function: " ) # Initializing list2 # list2 have three objects a, b, and c # and they all are numeric objects (same data type) list2 <- list (a=11: 12, sample ( c (1, 2, 5, 3), size=4, replace= FALSE ), c=40: 60) # Printing the length of list2 objects sapply (list2, length) # Printing the sum of elements # present in the list2 objects sapply (list2, sum) # Printing the mean of elements # present in the list2 objects sapply (list2, mean) # Printing the cumulative sum of # elements present in the list2 objects sapply (list2, cumsum) |
Output:
Example 3: We can use non-numeric objects also in a list but after applying operations like “mean” on the list we get the “NA” result in output since these operations work for numeric objects only.
R
print ( "Operations using lapply() function: " ) # Initializing list1 # list1 have three objects a, b, and c # and they all are numeric objects # (same data type) list1 <- list (a = 11: 12, b = c ( 'Geeks' , 'for' , 'Geeks' ), c = 40:60) # Printing the length of list1 objects lapply (list1, length) # Printing the mean of elements # present in the list1 objects lapply (list1, mean) print ( "Operations using sapply() function: " ) # Initializing list2 # list2 have three objects a, b, and c # and they all are numeric objects (same data type) list2 <- list (a = 11: 12, b = c ( 'Geeks' , 'for' , 'Geeks' ), c = 40:60) # Printing the length of list2 objects sapply (list2, length) # Printing the mean of elements # present in the list2 objects sapply (list2, mean) |
Output:
The lapply() and sapply() functions print NA for object b in list1 since b is a non-numeric object. R compiler gives a warning whenever we apply these operations on a list containing a number of non-numeric objects.