do.call()
作为基础函数,do.call的作用可以简单总结为:对列表中的每一项进行某个函数操作(rbind,cbind,sum,paste等)。简单的讲,do.call 的功能就是执行一个函数,而这个函数的参数呢,放在一个list里面, 是list的每个子元素。
在r中的介绍
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())
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 what is a character string and the arguments are symbols or quoted expressions.
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.
example
example-1
> tmp <- data.frame('letter' = letters[1:10], 'number' = 1:10, 'value' = c('+','-'))
> tmp
letter number value
1 a 1 +
2 b 2 -
3 c 3 +
4 d 4 -
5 e 5 +
6 f 6 -
7 g 7 +
8 h 8 -
9 i 9 +
10 j 10 -
> tmp[[1]]
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
> tmp[[2]]
[1] 1 2 3 4 5 6 7 8 9 10
> tmp[[3]]
[1] "+" "-" "+" "-" "+" "-" "+" "-" "+" "-"
> do.call("paste", c(tmp, sep = ""))
[1] "a1+" "b2-" "c3+" "d4-" "e5+" "f6-" "g7+" "h8-" "i9+" "j10-"
example-2
#批量处理GDC下载下来的临床数据
> library(XML)
> result <- xmlParse("./clinical/142aea0e-7a7b-4ac4-9dbb-0f62e2379599/nationwidechildrens.org_clinical.TCGA-W5-AA2O.xml")
>
> rootnode <- xmlRoot(result)
> xmlSize(rootnode)
[1] 2
>
> print(rootnode[1])
$admin
<admin:admin>
<admin:bcr xsd_ver="1.17">Nationwide Children's Hospital</admin:bcr>
<admin:file_uuid xsd_ver="2.6">1F436CF6-6CC3-4950-8B36-A05E0DE33F7B</admin:file_uuid>
<admin:batch_number xsd_ver="1.17">428.25.0</admin:batch_number>
<admin:project_code xsd_ver="2.7">TCGA</admin:project_code>
<admin:disease_code xsd_ver="2.6">CHOL</admin:disease_code>
<admin:day_of_dcc_upload xsd_ver="1.17">22</admin:day_of_dcc_upload>
<admin:month_of_dcc_upload xsd_ver="1.17">12</admin:month_of_dcc_upload>
<admin:year_of_dcc_upload xsd_ver="1.17">2016</admin:year_of_dcc_upload>
<admin:patient_withdrawal>
<admin:withdrawn>false</admin:withdrawn>
</admin:patient_withdrawal>
<admin:program xsi:nil="true" xsd_ver="2.7"/>
<admin:dbgap_registration_code xsi:nil="true" xsd_ver="2.7"/>
</admin:admin>
attr(,"class")
[1] "XMLInternalNodeList" "XMLNodeList"
> #print(rootnode[2])
> xmldataframe <- xmlToDataFrame(rootnode[2])
> head(t(xmlToDataFrame(rootnode[2])))
[,1]
additional_studies ""
tissue_source_site "W5"
patient_id "AA2O"
bcr_patient_barcode "TCGA-W5-AA2O"
bcr_patient_uuid "180A2B9C-9CAC-4D96-86E8-CD6B3B9386B5"
informed_consent_verified "YES"
>
> xmls = dir("clinical/",pattern = "*.xml$",recursive = T)
> cl = list()
> for(i in 1:length(xmls)){
+ result <- xmlParse(paste0("clinical/",xmls[[i]])) #批量读取
+ rootnode <- xmlRoot(result) #找到文件节点
+ cl[[i]] = xmlToDataFrame(rootnode[2]) #取第二个节点,并将其转化为数据框模式放进列表里
+ }
> clinical <- do.call(rbind,cl) #将列表中的数据框取出并按行合并
> clinical[1:3,1:3]
additional_studies tissue_source_site patient_id
1 W5 AA2Q
2 W5 AA2O
3 W5 AA39
do.cal类似一个循环,对list中每个对象进行函数操作,类似的还有函数还有apply家族的函数,待有时间分享一下学习经过。
网友评论