Purrr包
Hadley Wickham開發(fā)的,天生函數(shù)式編程范式,可以實現(xiàn)迭代列表,并對每個元素單獨應(yīng)用函數(shù)。盡管lapply函數(shù)在R的基礎(chǔ)包中也都實現(xiàn)了相
Hadley Wickham開發(fā)的,天生函數(shù)式編程范式,可以實現(xiàn)迭代列表,并對每個元素單獨應(yīng)用函數(shù)。
盡管lapply函數(shù)在R的基礎(chǔ)包中也都實現(xiàn)了相關(guān)的迭代功能,但Purrr包的函數(shù)在計算時間和編程耗時上更短,能使用管道符進一步操作。而且返回結(jié)果也更友好。
map函數(shù)
該包的基礎(chǔ)函數(shù),將函數(shù)應(yīng)用到列表的每個元素上,并返回相應(yīng)長度的一個列表。看起來與lapply函數(shù)類似,但map函數(shù)被設(shè)計來進行管道操作。
> theList <- list(A=matrix(1:9,3),B=1:5,C=matrix(1:4,2),D=2)n> lapply(theList,sum)n$An[1] 45nn$Bn[1] 15nn$Cn[1] 10nn$Dn[1] 2
map函數(shù)實現(xiàn)類似功能
> library(purrr)n> theList %>% map(sum)n$An[1] 45nn$Bn[1] 15nn$Cn[1] 10nn$Dn[1] 2
測試是否一樣
> identical(lapply(theList, sum),theList %>% map(sum))n[1] TRUE
map函數(shù)可以用管道操作。
> theList2 <- theListn> theList2[[1]][2,1] <- NAn> theList2[[2]][4] <- NAn> theList2 %>% map(sum)n$An[1] NAnn$Bn[1] NAnn$Cn[1] 10nn$Dn[1] 2
當有NA值時的結(jié)果。
> theList2 %>% map(function(x) sum(x,na.rm = T))n$An[1] 43nn$Bn[1] 11nn$Cn[1] 10nn$Dn[1] 2
通過定義匿名函數(shù)處理NA值。
> theList2 %>% map( sum,na.rm =TRUE)n$An[1] 43nn$Bn[1] 11nn$Cn[1] 10nn$Dn[1] 2
上面通過為sum函數(shù)提供額外的參數(shù)來解決NA值。
返回特定類型的map函數(shù)
> theList %>% map_int(NROW)nA B C D n3 5 2 1
這個函數(shù)對每個元素返回一個整數(shù),如果元素是一維的,返回其長度,如果是二維的,返回其行數(shù)。
> theList %>% map_int(mean)n錯誤: Can't coerce element 1 from a double to a integern>
上面返回結(jié)果不符合要求。
> theList %>% map_dbl(mean)n A B C D n5.0 3.0 2.5 2.0
這樣就可以。
> theList %>% map(class)n$An[1] "matrix" "array" nn$Bn[1] "integer"nn$Cn[1] "matrix" "array" nn$Dn[1] "numeric"
元素的類型。
> theList %>% map_lgl(function(x) NROW(x) < 3)n A B C D nFALSE FALSE TRUE TRUE
返回邏輯向量。
buildDF <- function(x){n data.frame(A=1:x,B=x:1)} #構(gòu)建有兩列的數(shù)據(jù)框nnnlistOfLengths <- list(3,4,1,5)nlistOfLengths %>% map(buildDF) #迭代列表,為每個元素創(chuàng)建一個數(shù)據(jù)框
map函數(shù)返回一個長度為4的列表,其中每個元素都是一個數(shù)據(jù)框。
[[1]]n A Bn1 1 3n2 2 2n3 3 1nn[[2]]n A Bn1 1 4n2 2 3n3 3 2n4 4 1nn[[3]]n A Bn1 1 1nn[[4]]n A Bn1 1 5n2 2 4n3 3 3n4 4 2n5 5 1
下面直接返回數(shù)據(jù)框類型
> listOfLengths %>% map_df(buildDF)n A Bn1 1 3n2 2 2n3 3 1n4 1 4n5 2 3n6 3 2n7 4 1n8 1 1n9 1 5n10 2 4n11 3 3n12 4 2n13 5 1
map_if在符合某種條件下才對列表中的元素進行修改
> theList %>% map_if(is.matrix,function(x) x*2)n$An [,1] [,2] [,3]n[1,] 2 8 14n[2,] 4 10 16n[3,] 6 12 18nn$Bn[1] 1 2 3 4 5nn$Cn [,1] [,2]n[1,] 2 6n[2,] 4 8nn$Dn[1] 2n
也可以用更formula創(chuàng)建簡單的匿名函數(shù),參數(shù)形式為.x和.y
> theList %>% map_if(is.matrix,~ .x*2)n$An [,1] [,2] [,3]n[1,] 2 8 14n[2,] 4 10 16n[3,] 6 12 18nn$Bn[1] 1 2 3 4 5nn$Cn [,1] [,2]n[1,] 2 6n[2,] 4 8nn$Dn[1] 2
數(shù)據(jù)框的迭代
從本質(zhì)上講數(shù)據(jù)框也是列表。計算數(shù)據(jù)集diamonds中數(shù)值型的列的平均。
> data(diamonds,package = 'ggplot2')n> diamonds %>% map_dbl(mean)n carat cut color clarity n 0.7979397 NA NA NA n depth table price x n 61.7494049 57.4571839 3932.7997219 5.7311572 n y z n 5.7345260 3.5387338
非數(shù)值列型的列返回NA。
> diamonds %>% summarise_each(funs(mean))n# A tibble: 1 x 10n carat cut color clarity depth table price xn <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>n1 0.798 NA NA NA 61.7 57.5 3933. 5.73n# ... with 2 more variables: y <dbl>, z <dbl>nWarning messages:n1: `summarise_each_()` was deprecated in dplyr 0.7.0.nPlease use `across()` instead.
上述函數(shù)返回一個單行數(shù)據(jù)框,而map_dbl返回一個數(shù)值型向量。
map函數(shù)的多輸入
兩個列表的元素作用在函數(shù)上
firstList <- list(A=matrix(1:16,4),B=matrix(1:16,2),C=1:5)nsecondList <- list(A=matrix(1:16,4),B=matrix(1:16,8),C=15:1)nsimpleFunc <- function(x,y){n NROW(x)+NROW(y)n}nmap2(firstList,secondList,simpleFunc)nn$An[1] 8nn$Bn[1] 10nn$Cn[1] 20
下面返回整型向量
> map2_int(firstList,secondList,simpleFunc)n A B C n 8 10 20
更一般的pmap函數(shù)
> pmap(list(firstList,secondList),simpleFunc)n$An[1] 8nn$Bn[1] 10nn$Cn[1] 20
類似地
> pmap_int(list(firstList,secondList),simpleFunc)n A B C n 8 10 20
參考文獻
R for Everyone:Advanced Analytics and Graphics,Lander,Jared著,2017-06-08
上一篇:MARVEL R 電機
下一篇:mvr多效蒸發(fā)器







