R语言do.call 函数用法详解
虽然R语言有类型很丰富的数据结构,但是很多时候数据结构比较复杂,那么基本就会用到list这种结构的数据类型。但是list对象很难以文本的形式导出,因此需要一个函数能快速将复杂的list结构扁平化成dataframe。这里要介绍的就是do.call函数。
这里是do.call 函数的官方文档:
| do.call {base} | R Documentation |
Execute a Function Call
Description
do.call constructs and executes a function call from a name or a function and a list of arguments to be passed to it.
Usage
do.call(what, args, quote = FALSE, envir = parent.frame())
Arguments
what |
either a function or a non-empty character string naming the function to be called. |
args |
a list of arguments to the function call. The |
quote |
a logical value indicating whether to quote the arguments. |
envir |
an environment within which to evaluate the call. This will be most useful if |
Details
If quote is FALSE, the default, then the arguments are evaluated (in the calling environment, not in envir). If quote is TRUE then each argument is quoted (see quote) so that the effect of argument evaluation is to remove the quotes – leaving the original arguments unevaluated when the call is constructed.
The behavior of some functions, such as substitute, will not be the same for functions evaluated using do.call as if they were evaluated from the interpreter. The precise semantics are currently undefined and subject to change.
Value
The result of the (evaluated) function call.
Warning
This should not be used to attempt to evade restrictions on the use of .Internal and other non-API calls.
References
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
简单的讲,do.call 的功能就是执行一个函数,而这个函数的参数呢,放在一个list里面, 是list的每个子元素。
看例子:
> tmp <- data.frame('letter' = letters[:], 'number' = :, 'value' = c('+','-'))
> tmp
letter number value
a +
b -
c +
d -
e +
f -
g +
h -
i +
j -
> tmp[[]]
[] a b c d e f g h i j
> tmp[[]]
[]
> tmp[[]]
[] + - + - + - + - + -
> do.call("paste", c(tmp, sep = ""))
[] "a1+" "b2-" "c3+" "d4-" "e5+" "f6-" "g7+" "h8-" "i9+" "j10-"
这里的tmp使用data.frame函数创建的,其实它本质上还是一个list,这里分别用[[]]符号显示他的三个元素,可以看到do.call函数把tmp的三个元素(三个向量)作为paste函数的参数。这个例子我们也可以这样写:
> paste(tmp[[1]],tmp[[2]],tmp[[3]], sep = "")
[1] "a1+" "b2-" "c3+" "d4-" "e5+" "f6-" "g7+" "h8-" "i9+" "j10-"
可以看到两种结果是一模一样的。
再举一个例子:
> number_add <- list(:, :)
> number_add
[[]]
[] [[]]
[] > add <- function(x,y) {x + y}
> add
function(x,y) {x + y} > do.call(add, number_add)
[]
> add(number_add[[]], number_add[[]])
[]
最后回到开头,假如说我们有一个list对象,这个对象里面是格式一致的dataframe,我们需要将这个list对象合并成一个总的dataframe并输出成文本文件,那么可以这样做:
> list1
[[]]
up down number
A a
B b
C c
D d
E e [[]]
up down number
A a
B b
C c
D d
E e [[]]
up down number
A a
B b
C c
D d
E e > do.call("rbind",list1)
up down number
A a
B b
C c
D d
E e
A a
B b
C c
D d
E e
A a
B b
C c
D d
E e
这里再推荐一个比较实用的函数族,apply族函数,有兴趣的朋友可以查阅相关资料。
R语言do.call 函数用法详解的更多相关文章
- C语言对文件的操作函数用法详解2
fopen(打开文件) 相关函数 open,fclose 表头文件 #include<stdio.h> 定义函数 FILE * fopen(const char * path,const ...
- C语言对文件的操作函数用法详解1
在ANSIC中,对文件的操作分为两种方式,即: 流式文件操作 I/O文件操作 一.流式文件操作 这种方式的文件操作有一个重要的结构FILE,FILE在stdio.h中定义如下: typedef str ...
- 转载 LayoutInflater的inflate函数用法详解
http://www.open-open.com/lib/view/open1328837587484.html LayoutInflater的inflate函数用法详解 LayoutInflater ...
- SQL中CONVERT()函数用法详解
SQL中CONVERT函数格式: CONVERT(data_type,expression[,style]) 参数说明: expression 是任何有效的 Microsoft® SQL Server ...
- php中setcookie函数用法详解(转)
php中setcookie函数用法详解: php手册中对setcookie函数讲解的不是很清楚,下面是我做的一些整理,欢迎提出意见. 语法: bool set ...
- eval()函数用法详解
eval()函数用法详解:此函数可能使用的频率并不是太高,但是在某些情况下具有很大的作用,下面就介绍一下eval()函数的用法.语法结构: eval(str) 此函数可以接受一个字符串str作为参数, ...
- delphi中Application.MessageBox函数用法详解
delphi中Application.MessageBox函数用法详解 Application.MessageBox是TApplication的成员函数,声明如下:functionTApplicati ...
- Go语言Slice作为函数参数详解
Go语言Slice作为函数参数详解 前言 首先要明确Go语言中实质只有值传递,引用传递和指针传递是相对于参数类型来说. 个人认为上诉的结论不对,把引用类型看做对指针的封装,一般封装为结构体,结构体是值 ...
- Python3中正则模块re.compile、re.match及re.search函数用法详解
Python3中正则模块re.compile.re.match及re.search函数用法 re模块 re.compile.re.match. re.search 正则匹配的时候,第一个字符是 r,表 ...
随机推荐
- 前端之BOM和DOM
BOM和DOM简介 BOM(Browser Object Model)是指浏览器对象模型,它使JavaScript有能力与浏览器进行“对话”. DOM(Document Object Model)是指 ...
- Spring(六):Spring&Struts2&Hibernate搭建的blog项目
经过断断续续的学习.累积,终于基于别人的开源blog项目,变成了自己的第一个相对完整点的blog项目. 计划暂时把这个blog程序暂停------有更多(工作中用到的)东西需要去做,因此学习SSH b ...
- scrapy选择器主要用法
# 命令行输入:scrapy shell +链接,会自动请求url,得到的相应默认为response,开启命令行交互模式 scrapy shell http://doc.scrapy.org/en/l ...
- hue集成hbase出现TSocket read 0 bytes
解决办法:修改hbase的配置文件 添加以下配置 https://stackoverflow.com/questions/20415493/api-error-tsocket-read-0-bytes ...
- Override与Overload
方法重写(Override) 方法重写是子类对父类(父类为抽象类)的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变.即外壳不变,核心重写! 方法的重写规则 1.参数列表必须完全与被重写 ...
- springboot测试、打包、部署
本文使用<springboot集成mybatis(一)>项目,依次介绍springboot测试.打包.部署. 大多数朋友是做后端的,也就是为其他系统或者前端UI提供Rest API服务. ...
- HDFS简介及相关概念
HDFS简介: HDFS在设计时就充分考虑了实际应用环境的特点,即硬件出错在普通服务集群中是一种常态,而不是异常. 因此HDFS主要实现了以下目标: 兼容廉价的硬件设备 HDFS设计了快速检测硬件故障 ...
- Hello——Java10新特性,请了解一下
2018年3月20日,Java 10 正式发布! 相关地址: 官方地址:http://www.oracle.com/technetwork/java/javase/downloads/index.ht ...
- Angular CLI 安装
安装Angular 官网的教程,因为国内网络环境原因,访问不了服务器,导致安装失败. 1.先安装NodeJs 安装教程:http://blog.csdn.net/zengmingen/article/ ...
- C++11 作用域内枚举
enum class MyEnum{ P1 = , P2, P3, P4, P5 }; MyEnum myEnum = MyEnum::P2; 使用作用域的方式获取并限定P2的值.之所以要使用作用域, ...