R 语言--purrr包

神器purrr包是Hadley Wickham大神编写的高级函数编程语言包,它可以填充R功能性编程中的缺失部分,使得你的编程更加函数化。

purrr包提供了大量的类似map的函数。可以服务于减少循环、处理嵌套数据、多模型等应用需求。

map()函数
library("purrr")    #加载包
data(mtcars)        #使用数据集mtcars
head(mtcars)        #查看mtcars数据的前五行
dim(mtcars)         #查看数据mtcars的维度
##求mtcars数据的均值
map_dbl(mtcars, mean)
mtcars %>% map_dbl(mean)

640?wx_fmt=png&wxfrom=5&wx_lazy=1

##对mtcars的数据进行标准化

mtcars %>% map(function(x) (x - mean(x)/max(x)-min(x)))   # list
mtcars %>% map_df(function(x) (x - mean(x)/max(x)-min(x)))# data.frame

0?wx_fmt=png

map2()
n <- list(4,5,6)
m <- list(1,2,3)
map2(m,n, `+`)

0?wx_fmt=png

by_cyl <- mtcars %>% split(.$cyl)
mods <- by_cyl %>% map(~ lm(mpg ~ wt, data = .))
map2(mods, by_cyl, predict)

0?wx_fmt=png

pmap()
x <- list(1, 10, 100)
y <- list(1, 2, 3)
z <- list(5, 50, 500)
pmap(list(x, y, z), sum)

0?wx_fmt=png

pmap(list(x, y, z), function(a, b ,c) a / (b + c))

0?wx_fmt=png

accumulate()        # 行列长度不变
mtcars[1,]
mtcars[1,] %>% accumulate(`+`)       # 从左向右

0?wx_fmt=png

mtcars[1,] %>% accumulate(`*`) 

0?wx_fmt=png

mtcars[1,] %>% accumulate_right(`+`) # 从右向左

0?wx_fmt=png

mtcars[1,] %>% accumulate_right(`*`)

0?wx_fmt=png

reduce ()      # 获得一个值
mtcars[,1]

0?wx_fmt=png

mtcars[,1] %>% reduce(`+`)       # 从左向右

0?wx_fmt=png

mtcars[,1] %>% reduce(`*`) 

0?wx_fmt=png

mtcars[,1] %>% reduce_right(`+`) # 从右向左

0?wx_fmt=png

mtcars[,1] %>% reduce_right(`*`)

0?wx_fmt=png

detect () # 找到第一个匹配的值
mtcars[2,]
mtcars[2,] %>% detect(~. >50)

0?wx_fmt=png

# detect_index()   并返回它的位置
mtcars[2,] %>% detect_index(~. >50)

0?wx_fmt=png

some()  列表中的某些元素是否满足要求? 返回结果TRUE/FALSE

every() 列表中的每个元素是否满足要求?

mtcars %>% some(is_numeric)

0?wx_fmt=png

mtcars %>% some(is_character)

0?wx_fmt=png

mtcars %>% every(is_numeric)

0?wx_fmt=png

mtcars %>% every(is_character)

0?wx_fmt=png

x <- list(1:5,'R','ruby','python')
x %>% every(is_character)

0?wx_fmt=png

x %>% some(is_character)

0?wx_fmt=png

x %>% every(is_numeric)

0?wx_fmt=png

x %>% some(is_numeric)

0?wx_fmt=png

head_while() 找到所有满足头部匹配的值

 tail_while() 找到所有满足尾部匹配的值

mtcars[1,]
mtcars[1,] %>% head_while(~. >5)

640?wx_fmt=png&wxfrom=5&wx_lazy=1

mtcars[1,] %>% head_while(~. >15)

640?wx_fmt=png&wxfrom=5&wx_lazy=1

mtcars[,1]
mtcars[,1] %>% tail_while(~. >25)

0?wx_fmt=png

mtcars[,1] %>% tail_while(~. >21)

0?wx_fmt=png

fun <- function(x) x >= 1
head_while(5:-5, fun)

0?wx_fmt=png

head_while(-1:-5, fun)

0?wx_fmt=png

x <- sample(10)
x
x %>% head_while(~. >5)

0?wx_fmt=png

x %>% tail_while(~. >5)

0?wx_fmt=png

transpose() 将列表对变成列表/将一个列表对成一个清单

mtcars %>% transpose()
mtcars %>% transpose() %>%simplify_all()

0?wx_fmt=png

x <- list(list(a=1,b=2),list(a=3,b=4))
x
x %>% transpose()

0?wx_fmt=png

x %>% transpose() %>%simplify_all()

0?wx_fmt=png

ll <- list(
  list(x = 1, y = "one"),
  list(z = "deux", x = 2)
)
ll %>% transpose()

0?wx_fmt=png

ll %>% transpose() %>%simplify_all()

0?wx_fmt=png

keep()                     使用函数保留元素

discard()                  使用函数放弃元素  保存于放弃是对立的

head(mtcars)
mtcars[1:5,] %>% keep(function(x) mean(x) > 10)

0?wx_fmt=png

mtcars[1:5,] %>% keep(function(x) mean(x) < 10)

0?wx_fmt=png

mtcars[1:5,] %>% discard(function(x) mean(x) < 10)

0?wx_fmt=png

mtcars[1:5,] %>% discard(function(x) mean(x) > 10)

0?wx_fmt=png

rep(3, 2) %>%                # rep(a,time=)
  map(rnorm, n=5) %>%          #每行个数 
  keep(function(x) mean(x) > 2)

0?wx_fmt=png

rep(3, 2) %>%                # rep(a,time=)
  map(rnorm, n=5) %>%          #每行个数 
  discard(function(x) mean(x) >4)

0?wx_fmt=png

safely     #函数包装功能,不通过打印输出消息,警告和错误

# safely()   返回一个带有组件结果和错误的列表

# quietly()  返回一个包含组件结果/输出/警告和消息

# possibly() 包装函数在发生错误时使用默认值(否则)

fun_log <- safely(log)        #log        
str(fun_log(10))

0?wx_fmt=png

list("a",100,9) %>% map(fun_log)

0?wx_fmt=png

list(99,100,9) %>% map(fun_log) %>% transpose()

0?wx_fmt=png

fun_log(mtcars[1,])

0?wx_fmt=png

mean_fun <-quietly(mean)      #mean
mean_fun(c(1,3)) %>% transpose()

0?wx_fmt=png

mean_fun(mtcars[,1])

0?wx_fmt=png

mean(mtcars[,1])

0?wx_fmt=png

x <- list(1, 10, "a")         #log
x %>% map_dbl(possibly(log, NA_real_))  # otherwise

0?wx_fmt=png

walk 无形的返回第一个参数,经常与管道函数并用

# walk()
# pwalk()
# walk2()
x <- list(1, "a", 3)
x %>% walk(print)

0?wx_fmt=png


猜你喜欢

转载自blog.csdn.net/u011596455/article/details/79773991
今日推荐