7.-顏林林-R高級編程技巧及Rcpp的介紹.ppt_第1頁
7.-顏林林-R高級編程技巧及Rcpp的介紹.ppt_第2頁
7.-顏林林-R高級編程技巧及Rcpp的介紹.ppt_第3頁
7.-顏林林-R高級編程技巧及Rcpp的介紹.ppt_第4頁
7.-顏林林-R高級編程技巧及Rcpp的介紹.ppt_第5頁
已閱讀5頁,還剩37頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

R高級編程技巧及Rcpp的介紹 顏林林北京大學(xué)生物信息中心2011年5月28日 R語言的特點(diǎn) FREE 開源 免費(fèi) 自由 靈活 anrow a 截至2011年5月28日 CRAN上總計(jì)可用軟件包數(shù) 3008 遵守各種不同開源協(xié)議 R語言的特點(diǎn) FREE 開源 免費(fèi) 自由 靈活 sort table gsub a License decreasing T 高校 科研機(jī)構(gòu) 企事業(yè)單位 金融 通訊 醫(yī)藥 生物 環(huán)境 R語言的廣泛運(yùn)用 COS論壇 創(chuàng)辦于2006年5月19日截至2011年5月28日 整個論壇 主題 16 000 回復(fù) 109 000 S Plus R語言 版 主題 4 000 回復(fù) 27 000 學(xué)習(xí)R的一個捷徑 在COS上回帖 論壇中一些常見關(guān)于R的問題 如何畫XX圖 如何實(shí)現(xiàn)XX功能 安裝時遇到XX問題如何解決 如何提高R程序的運(yùn)行效率 153分鐘學(xué)會R 正題 如何讓R飛起來 R高級編程技巧向量運(yùn)算軟件包的使用Rcpp的介紹結(jié)合不同語言的優(yōu)勢拓展R的運(yùn)用范圍 向量運(yùn)算 是一種習(xí)慣 循環(huán)的思維方式 for pin參加R大會的人 發(fā)資料給p 發(fā)胸牌給p 發(fā) 給p 生活中的思維方式 發(fā)資料 胸牌等給參加R大會的人 能用更少的話說清楚 就別羅嗦 aa 2 sin a R是一門解釋型語言 a 向量運(yùn)算 b 循環(huán) bfor iinseq along a b i sin a i R是一門解釋型語言 system time replicate 10000 bbsystem time replicate 10000 for iinseq along a b i sin a i usersystemelapsed6 150 026 44 把苦力活交給底層 盡量避免使用循環(huán) 如何掌握好向量運(yùn)算 R語言基本數(shù)據(jù)類型vector matrix array data frame list 元素的提取方法a 1 3 a 2 a a 3 a c x y 數(shù)據(jù)類型之間的互相轉(zhuǎn)換as matrix as vector as character apply系列函數(shù)apply sapply lapply tapply 其他各種函數(shù)sum mean cumsum combn R語言元素類型 整數(shù) integer 實(shí)數(shù) numeric double 復(fù)數(shù) complex 字符 character 邏輯 logical 原始數(shù)據(jù) raw 因子 factor 1 10c 1 1 3 14 10 c 1 i 3 2i c a b COS c TRUE FALSE as raw 48 as factor letter 1 3 R語言基本數(shù)據(jù)類型 向量 vector 列表 list 矩陣 matrix 數(shù)組 array 數(shù)據(jù)框 data frame R語言基本數(shù)據(jù)類型 向量 vector 列表 list 矩陣 matrix 數(shù)組 array 數(shù)據(jù)框 data frame 一維 同類型元素一維 不同類型元素二維 同類型元素二維或多維 同類型元素二維 每列元素同類型 學(xué)習(xí)R函數(shù)時應(yīng)注意 apply系列函數(shù) 函數(shù)apply mapply m 1 sum 相當(dāng)于rowSums m 1 1620 apply m 2 sum 相當(dāng)于colSums m 1 371115 apply m 1 2 function x x 2 1 2 3 4 1 0 51 52 53 5 2 1 02 03 04 0 函數(shù)lapply和sapply llapply l sum 返回列表 a 1 15 b 1 40 sapply l sum 返回向量ab1540 函數(shù)sapply和vapply Xsapply X fivenum 1 2 3 1 1 01 01 2 1 51 52 3 2 02 53 4 2 53 54 5 3 04 05 函數(shù)sapply和vapply vvapply X fivenum v 1 2 3 Min 1 01 011stQu 1 51 52Median2 02 533rdQu 2 53 54Max 3 04 05 函數(shù)tapply dc class d class d age class d gender 1 data frame numeric factor tapply d age d gender mean FemaleMale24 024 5 函數(shù)mapply mapply function x y seq len x y c a 1 b 2 c 3 namesfromfirstc A 10 B 0 C 10 a 1 11 b 1 12 c 1 9 8 7 函數(shù)rapply Xrapply X function x x ab cd 3 14159265358979 1 atest 舉例 求DNA互補(bǔ)鏈 DNA由A C T G組成DNA兩條鏈方向相反成對堿基 A與T配對 C與G配對已知其中一條為 ACTGAAGTGC 求另一條序列 舉例 求DNA互補(bǔ)鏈 方法一 循環(huán)revcom1 function DNA result for iinnchar DNA 1 n substr DNA i i if n A result paste result T sep if n T result paste result A sep if n C result paste result G sep if n G result paste result C sep result 舉例 求DNA互補(bǔ)鏈 方法二 apply revcom2 function DNA s strsplit DNA 1 s sapply s function x if x A return T if x T return A if x G return C if x C return G paste rev s collapse 方法三 names revcom3 function DNA tr c A C G T names tr rev tr paste tr rev strsplit DNA 1 collapse trTGCA A C G T 舉例 求DNA互補(bǔ)鏈 方法比較 library rbenchmark benchmark revcom1 DNA revcom2 DNA revcom3 DNA columns c test replications elapsed relative order relative replications 1000 testreplicationselapsedrelative3revcom3 DNA 10000 1631 0000002revcom2 DNA 10000 5973 6625771revcom1 DNA 10000 6243 828221 R的靈活性 R是什么語言寫成的 C C Fortran RR的外部擴(kuò)展WritingRExtensions R與各種語言 R bash RscriptRDCOMrJavarpy rpy2EmbededRserve RAPI Rcpp R與C R 解釋型語言 無需編譯不需要其它文件弱類型語言靈活性好運(yùn)行速度較慢其他特性向量運(yùn)算大量統(tǒng)計(jì)和繪圖函數(shù) C 編譯型語言 需要事先編譯需要頭文件和庫文件強(qiáng)類型語言靈活性相對較差運(yùn)行速度快其他特性面向?qū)ο竽0迮c泛型編程 從C到C 字符串 數(shù)組等類型內(nèi)存的管理模板和泛型編程STL StandardTemplateLibrary Rcpp相關(guān)歷史 RQuantLibRcppTemplateRservercppbind RAbstraction RObjects CXXRRAPIRcpp RInside RProtoBuf Rcpp的運(yùn)用 在R中調(diào)用C 模塊 Rcpp 用C 實(shí)現(xiàn)計(jì)算功能通過C 調(diào)用其他軟件庫在C 中使用R RInside 向量運(yùn)算 STL 使用R的統(tǒng)計(jì)函數(shù)使用R的繪圖函數(shù) 舉例 Rcpp和inline帶來的速度 f function n x 1 for iin1 n x 1 1 x g function n x 1 for iin1 n x 1 1 x h function n x 1 for iin1 n x 1 x 1 j function n x 1 for iin1 n x 1 1 x k function n x 1 for iin1 n x 1 1 x library rbenchmark N 1e5benchmark f N 1 g N 1 h N 1 j N 1 k N 1 columns c test replications elapsed relative order relative replications 10 舉例 Rcpp和inline帶來的速度 f function n x 1 for iin1 n x 1 1 x g function n x 1 for iin1 n x 1 1 x h function n x 1 for iin1 n x 1 x 1 j function n x 1 for iin1 n x 1 1 x k function n x 1 for iin1 n x 1 1 x testreplicationselapsedrelative5k N 1 106 1031 0000001f N 1 106 4261 0529254j N 1 106 8351 1199412g N 1 107 3391 2025233h N 1 109 2261 511716 舉例 Rcpp和inline帶來的速度 library inline src ns doublex as xs for inti 0 i n i x 1 1 x returnwrap x l cxxfunction signature ns integer xs numeric body src plugin Rcpp 舉例 Rcpp和inline帶來的速度 benchmark f N 1 g N 1 h N 1 j N 1 k N 1 l N 1 columns c test replications elapsed relative order relative eplications 10 testreplicationselapsedrelative6l N 1 100 0271 00005k N 1 106 190229 25931f N 1 106 379236 25934j N 1 106 808252 14812g N 1 107 267269 14813h N 1 109 184340 1481 舉例 RcppAPI includeRcppExportSEXPfoo SEXPa SEXPb Rcpp NumericVectorxa a Rcpp NumericVectorxb b intn

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論