sapply lapply

The family of apply() functions in R is used to apply user-defined functions to the elements of complex structures like matrices, lists or data frames.

R中的apply()函数族用于将用户定义的函数应用于复杂结构的元素,例如矩阵 , 列表或数据帧 。

These functions help a lot in simplifying your code and making it more readable. Moreover, they are compatible with parallel processing as well. Let us look at each function with detailed examples.

这些功能在简化代码和提高可读性方面有很大帮助。 而且,它们也与并行处理兼容。 让我们看一下每个函数的详细示例。

R编程中的apply()函数 (The apply() function in R Programming)

The apply() function in R is used in case of matrices to apply a user-specified function on the rows or columns of the matrix. The following is the general syntax for apply() function.

R中的apply()函数用于矩阵的情况下,以将用户指定的函数应用于矩阵的行或列。 以下是apply()函数的常规语法。


apply(matrix, code, f, fargs)

In the above form, matrix is the matrix object which we are using the apply() function for. Code represents whether we wish to apply the function for rows (code set to 1) or columns (code set to 2). F represents the function we need to apply and fargs are the arguments to pass to the function.

在上述形式中, matrix是我们正在使用apply()函数的矩阵对象。 Code表示我们是希望将函数应用于行(代码设置为1)还是列(代码设置为2)。 F表示我们需要应用的函数,而fargs是传递给该函数的参数。

Let us first define a matrix to illustrate the apply() function.

让我们首先定义一个矩阵来说明apply()函数。


> x <-matrix(c(4,5,6,10,12,16),nrow=2,ncol=3)
> x[,1] [,2] [,3]
[1,]    4    6   12
[2,]    5   10   16

Suppose that we wish to perform a very specific function upon each of these elements, like squaring first, then dividing by 3 and multiplying by 4. Let us define a function that does this.

假设我们希望对每个元素执行非常特定的功能,例如先平方,然后除以3再乘以4。让我们定义一个执行此功能的函数。


> f <-function(x)
+ {
+   return (x^2*3/4)
+ }
> f(5)
[1] 18.75

Now, in order to apply this function to all the rows/columns in a matrix, we call the apply() function.

现在,为了将此函数应用于矩阵中的所有行/列,我们调用了apply()函数。


> apply(x, 2, f)[,1] [,2] [,3]
[1,] 12.00   27  108
[2,] 18.75   75  192

The function gets conveniently applied to each element in the matrix without calling it in a loop. The apply() function in R doesn’t provide any speed benefit in execution but helps you write a cleaner and more compact code.

该函数可以方便地应用于矩阵中的每个元素,而无需在循环中调用它。 R中的apply()函数在执行时没有任何速度上的好处,但是可以帮助您编写更简洁,更紧凑的代码。

R编程中的sapply()和lapply()函数 (sapply() and lapply() functions in R Programming)

使用清单 (Working with Lists)

The lapply() function in R is short for list apply. This works in a manner similar to the apply() function above, but uses lists instead of matrices.

R中的lapply()函数是列表应用的缩写。 它的工作方式类似于上面的apply()函数,但是使用列表而不是矩阵。

Let us look at an example:

让我们看一个例子:


> mylist <- list(c(1,2,3,4),c(10,20,30,40),c(5,5,5,5))
> lapply(mylist,mean)
[[1]]
[1] 2.5[[2]]
[1] 25[[3]]
[1] 5

The list mylist is a list of 3 vectors. We wish to apply a mean function to each one of the vectors. This is done by calling lapply(mylist,mean) that returns the mean values of the three constituent vectors.

列表mylist是3个向量的列表。 我们希望对每个向量应用均值函数。 这可以通过调用lapply(mylist,mean) ,该函数返回三个组成向量的平均值。

Similarly, sapply() function in R is short for for simplified apply. Instead of obtaining a separate mean value for each vector, sapply returns a vector containing the mean values.

同样,R中的sapply()函数是简化应用的简称。 sapply返回每个矢量的单独平均值, sapply返回一个包含平均值的矢量。


> sapply(mylist,mean)
[1]  2.5 25.0  5.0

在R中使用数据框 (Working with Data Frames in R)

Since data frames can be treated as a special case of lists, the functions lapply() and sapply() work in both cases. Let us look at an example.

由于数据帧可以视为列表的特殊情况,因此函数lapply()sapply()在这两种情况下都可以工作。 让我们来看一个例子。

Let us create a data frame first and then apply a sort() function on it using the lapply() function in R.

让我们首先创建一个数据帧,然后使用R中的lapply()函数在其上应用sort()函数。


names <- c("Adam","Antony","Brian","Carl","Doug")
ages <- c(23,22,24,25,26)
playerdata <- data.frame(names,ages,stringsAsFactors = FALSE)#Apply a sort function on the dataframe
> lapply(playerdata,sort)
$names
[1] "Adam"   "Antony" "Brian"  "Carl"   "Doug"  $ages
[1] 22 23 24 25 26

The function returns both the columns of the data frame in a sorted order separately.

该函数以排序顺序分别返回数据框的两列。

Similarly, calling sapply() provides a compact list with each column sorted separately.

同样,调用sapply()会提供一个紧凑的列表,其中每个列都单独排序。


> sapply(playerdata,sort)names    ages
[1,] "Adam"   "22"
[2,] "Antony" "23"
[3,] "Brian"  "24"
[4,] "Carl"   "25"
[5,] "Doug"   "26"

tapply()函数 (tapply() function)

The tapply() function also belongs to the same family but used only in case of factors. This is best explained with an example. Suppose we have the salaries of employees in a company in the form of a vector and their respective means of transport in a factor. Suppose that we wish to calculate what is the average salary of each group using a specific means of transport.

tapply()函数也属于同一族,但仅在有factor的情况下使用 。 最好用一个例子来解释。 假设我们以矢量的形式获得公司员工的薪水,并将他们各自的运输方式作为一个因素。 假设我们希望使用特定的运输方式来计算每个组的平均工资。

The tapply() function in R programming can be called for this purpose using the R’s built-in mean function.

为此,可以使用R的内置均值函数来调用R编程中的tapply()函数。


> salaries <-c(25000,30000,45000,66000,20000,50000,35000,20000,15000)
> transport <-c('Bus','Car','Bus','Car','Metro','Metro','Bus','Bus','Metro')
> tapply(salaries,transport,mean)Bus      Car    Metro
31250.00 48000.00 28333.33

As you can observe, the tapply() function in R outputs a well-formatted mean of the salaries with the means of transport as columns. Also, notice how it only accounts for unique values from the transport vector automatically.

如您所见,R中的tapply()函数以一种运输方式作为列输出格式合理的薪水平均值。 另外,请注意它是如何仅自动考虑传输向量中的唯一值的。

翻译自: https://www.journaldev.com/35996/apply-sapply-lapply-tapply-functions-in-r

sapply lapply

sapply lapply_R编程中的apply(),sapply(),lapply()和tapply()函数相关推荐

  1. android layout(l, t, r, b);,服务器里的a,t,l,r,b是什么意思? Android编程中关于layout(l,t,r,b)函数的问题...

    导航:网站首页 > 服务器里的a,t,l,r,b是什么意思? Android编程中关于layout(l,t,r,b)函数的问题 服务器里的a,t,l,r,b是什么意思? Android编程中关于 ...

  2. C和C++编程中static关键字的含义-修饰函数和变量

    static用来修饰一个函数的时候,表示的意思如下: 1.表示该方法只在本c文件中有效.在其他c文件中无法访问这个方法. 2.static的方法的定义一般不放在.h文件中,因为这个方法只在一个c文件中 ...

  3. jquery 中 fn.apply(this, arguments)是什么函数?有什么作用?能举个例子吗

    1 function Person(name){ 2 this.name=name; 3 this.sayname=function (){ 4 alert(this.name); 5 } 6 } 7 ...

  4. Python函数式编程中map()、reduce()和filter()函数的用法

    Python中map().reduce()和filter()三个函数均是应用于序列的内置函数,分别对序列进行遍历.递归计算以及过滤操作.这三个内置函数在实际使用过程中常常和"行内函数&quo ...

  5. apply、lapply、sapply 与parApply、parApply、parSapply的区别

    parApply.parApply.parSapply是parallel包里面的 apply.lapply.sapply是base包里面自带的. parApply.parApply.parSapply ...

  6. Function中的apply函数的应用

    最近在研究PureMVC源码的过程中发现Function顶级包中的apply函数蛮有意思的,通过这个函数,可以及时的调用不同类中的同一个函数,这是该函数的格式:apply(thisArg:* , ar ...

  7. 函数式编程中的重要概念

    函数式编程中的重要概念 函数式编程范式的意义 函数类型与高阶函数 部分函数 柯里化 闭包 递归 记忆化 原文地址 函数式编程范式的意义 在众多的编程范式中,大多数开发人员比较熟悉的是面向对象编程范式. ...

  8. 函数式编程中的组合子

    函数式编程是一个比较大的话题,里面的知识体系非常的丰富,在这里我并不想讲的特别的详细.为了应对实际中的应用,我们讲一下函数式编程中最为实用的应用方式--组合子.组合子本身是一种高阶函数,他的特点就是将 ...

  9. Scala中的apply方法

    1.apply的场景 Scala中实例化一个类,可以不用new , 其原因 其实就是 apply 方法,具体原因 后边再讲: 在上句中Scala 生成Array对象, 会自动去调用 Array 这个伴 ...

最新文章

  1. 引导分区 pbr 数据分析_如何在1小时内引导您的分析
  2. 八种常见排序方法总结 C/C++代码实现
  3. 课后作业-阅读任务-阅读提问-3
  4. React 教程:快速上手指南
  5. Hadoop教程(五):Flume、Sqoop、Pig、Hive、OOZIE
  6. Mongodb安装搭建Replica Set+Sharding集群
  7. Java 和操作系统交互,你猜会发生什么?
  8. 数据库——环境初建改端口和密码(转)
  9. C# 8 新特性 - 异步流 Asynchronous Streams
  10. python kotlin_Java和Python中类似Kotlin的生成器,续:附加参数
  11. mysql中关于group的语句指令_mysql不支持group by的解决方法小结
  12. Linux系列-Red Hat5平台下的DHCP服务搭建
  13. CSS3控制背景图片大小
  14. 分享119个Android手机应用源代码总有一个是你想要的
  15. gentoo AR9285 BCM57780 安装驱动
  16. 利用Echarts+阿里云地图选择器绘制可交互的行政区划地图
  17. 二十一日——美国寻梦
  18. boost斩波电路控制系统C语言,Boost升压斩波电路[精华]
  19. 集合 -- 如何安全删除 HashMap 中的元素
  20. 红帽子安装oracle,红帽子AS4安装oracle9i

热门文章

  1. 一个图片轮换效果的JS
  2. [转载] numpy.arctan, math.atan, math.atan2的区别
  3. [转载] 比较器(Comparable和Comparator)、自然排序、定制排序
  4. 用MySQLdb操作数据库流程示例:
  5. 第三节基础篇—SQL的约束
  6. 【转】MEF程序设计指南四:使用MEF声明导出(Exports)与导入(Imports)
  7. CalendarDemo Calendar 类的创建及用法
  8. 路漫漫其修远兮,吾要上下左右前后而求索
  9. 一步一步教你安装Nginx+PHP+Mysql
  10. Visual C++ 2011-5-18