📜  R中lapply()与sapply()之间的区别

📅  最后修改于: 2022-05-13 01:55:39.532000             🧑  作者: Mango

R中lapply()与sapply()之间的区别

R 编程语言中的列表可以作为参数传递给lapply() sapply()函数。对列表所包含的对象中的元素执行一些一般操作,例如计算总和、累积总和、均值等是非常有帮助的。

拉应用()

在 R 中使用“for”循环遍历列表或向量会占用大量内存,而且速度也很慢。在处理大型数据集并对其进行迭代时,不建议使用 for 循环。 R 提供了许多用于循环操作的列表的替代方法,这些方法在命令行交互工作时非常有用。 lapply()函数是这些函数之一,它用于在列表上应用函数。

lapply()函数与列表一起使用并执行以下操作:

  • lapply(List, length):返回列表中存在的对象的长度,List。
  • lapply(List, sum):返回列表List中对象所持有的元素的总和。
  • lapply(List, mean):返回列表中对象所持有的元素的平均值,List。
  • lapply(List, cumsum):返回列表中存在的对象所持有的元素的累积总和,List。

应用()

此函数还用于在列表上应用函数,但具有简化的结果。

lapply()函数与列表一起使用并执行如下操作:

  • sapply(List, length):返回列表中存在的对象的长度,List。
  • sapply(List, sum):返回列表 List 中的对象所持有的元素的总和。
  • sapply(List, mean):返回列表中的对象所持有的元素的平均值,List。
  • sapply(List, cumsum):返回列表中存在的对象所持有的元素的累积总和,List。

lapply() 和 sapply() 函数的区别:

lapply()函数将输出显示为列表,而 sapply()函数将输出显示为 向量。 lapply() 和 sapply() 函数用于在对象列表中执行一些操作。 R 中的 sapply()函数在返回的输出中比 lapply() 更有效,因为 sapply() 将值直接存储到向量中。

示例 1: lapply()函数将输出作为列表返回,而 sapply()函数将输出作为向量返回

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)


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)


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)


R
print("Operations using sapply() function: ")
 
# list1 have three objects a, b, and c
# and they all are numeric objects (same data type)
list1 <- list(a = 11:12, b = 1:10, c = 40:60)
 
# Printing the length of list1 objects as a list
sapply(list1, length, simplify = FALSE)
 
# Printing the sum of elements present
# in the list1 objects as a list
sapply(list1, sum, simplify = FALSE)
 
# Printing the mean of elements present
# in the list1 objects as a list
sapply(list1, mean, simplify = FALSE)
 
# Printing the cumulative sum of elements
# present in the list1 objects as a list
sapply(list1, cumsum, simplify = FALSE)


输出:

示例 2:正如您在输出中看到的,lapply()函数将输出作为列表返回,而 sapply()函数将输出作为向量返回。

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)

输出:

示例 3:我们也可以在列表中使用非数字对象,但是在对列表应用诸如“mean”之类的操作后,我们会在输出中得到“ NA”结果,因为这些操作仅适用于数字对象。

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)

输出:

lapply() 和 sapply() 函数为 list1 中的对象 b 打印NA ,因为 b 是非数字对象。每当我们对包含许多非数字对象的列表应用这些操作时,R 编译器都会发出警告。

使用 sapply()函数将输出作为列表返回

sapply()函数接受第三个参数,使用它我们可以将输出作为列表而不是向量返回。

R

print("Operations using sapply() function: ")
 
# list1 have three objects a, b, and c
# and they all are numeric objects (same data type)
list1 <- list(a = 11:12, b = 1:10, c = 40:60)
 
# Printing the length of list1 objects as a list
sapply(list1, length, simplify = FALSE)
 
# Printing the sum of elements present
# in the list1 objects as a list
sapply(list1, sum, simplify = FALSE)
 
# Printing the mean of elements present
# in the list1 objects as a list
sapply(list1, mean, simplify = FALSE)
 
# Printing the cumulative sum of elements
# present in the list1 objects as a list
sapply(list1, cumsum, simplify = FALSE)

输出:

正如我们在输出中看到的,sapply()函数将输出作为列表而不是向量返回。因此,每当我们需要将输出打印为列表而不是向量时,我们总是可以将第三个参数传递给 sapply()函数。