雖然R語言有類型很豐富的數(shù)據(jù)結(jié)構(gòu)带兜,但是很多時(shí)候數(shù)據(jù)結(jié)構(gòu)比較復(fù)雜,那么基本就會用到list這種結(jié)構(gòu)的數(shù)據(jù)類型备徐。但是list對象很難以文本的形式導(dǎo)出痴晦,因此需要一個(gè)函數(shù)能快速將復(fù)雜的list結(jié)構(gòu)扁平化成dataframe。這里要介紹的就是do.call函數(shù)故慈。
這里是do.call 函數(shù)的官方文檔:
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
whateither a function or a non-empty character string naming the function to be called.
argsalistof arguments to the function call. Thenamesattribute ofargs?gives the argument names.
quotea logical value indicating whether to quote the arguments.
enviran environment within which to evaluate the call. This will be most useful ifwhat?is a character string and the arguments are symbols or quoted expressions.
Details
IfquoteisFALSE, the default, then the arguments are evaluated (in the calling environment, not inenvir). IfquoteisTRUEthen each argument is quoted (seequote) 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 assubstitute, will not be the same for functions evaluated usingdo.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 的功能就是執(zhí)行一個(gè)函數(shù),而這個(gè)函數(shù)的參數(shù)呢察绷,放在一個(gè)list里面, 是list的每個(gè)子元素。
看例子:
> tmp <- data.frame('letter'= letters[1:10],'number'=1:10,'value'= c('+','-'))> tmp
? letter number value1a1+2b2-3c3+4d4-5e5+6f6-7g7+8h8-9i9+10j10-> tmp[[1]]
[1] a b c d e f g h i j> tmp[[2]]
[1]12345678910> tmp[[3]]
[1] + - + - + - + - + ->do.call("paste", c(tmp, sep =""))
[1]"a1+""b2-""c3+""d4-""e5+""f6-""g7+""h8-""i9+""j10-"
這里的tmp使用data.frame函數(shù)創(chuàng)建的津辩,其實(shí)它本質(zhì)上還是一個(gè)list拆撼,這里分別用[[]]符號顯示他的三個(gè)元素,可以看到do.call函數(shù)把tmp的三個(gè)元素(三個(gè)向量)作為paste函數(shù)的參數(shù)喘沿。這個(gè)例子我們也可以這樣寫:
>paste(tmp[[1]],tmp[[2]],tmp[[3]], sep = "")
[1] "a1+"? "b2-"? "c3+"? "d4-"? "e5+"? "f6-"? "g7+"? "h8-"? "i9+"? "j10-"
可以看到兩種結(jié)果是一模一樣的闸度。
再舉一個(gè)例子:
> number_add <- list(101:110,1:10)> number_add
[[1]]
[1]101102103104105106107108109110[[2]]
[1]12345678910> add <-function(x,y) {x + y}> addfunction(x,y) {x + y}>do.call(add, number_add)
[1]102104106108110112114116118120> add(number_add[[1]], number_add[[2]])
[1]102104106108110112114116118120
最后回到開頭,假如說我們有一個(gè)list對象蚜印,這個(gè)對象里面是格式一致的dataframe莺禁,我們需要將這個(gè)list對象合并成一個(gè)總的dataframe并輸出成文本文件,那么可以這樣做:
> list1
[[1]]
? up down number1A? ? a12B? ? b23C? ? c34D? ? d45E? ? e5[[2]]
? up down number1A? ? a12B? ? b23C? ? c34D? ? d45E? ? e5[[3]]
? up down number1A? ? a12B? ? b23C? ? c34D? ? d45E? ? e5>do.call("rbind",list1)
? up down number1A? ? a12B? ? b23C? ? c34D? ? d45E? ? e56A? ? a17B? ? b28C? ? c39D? ? d410E? ? e511A? ? a112B? ? b213C? ? c314D? ? d415E? ? e5