Re: [问题] Magrittr 小问题

楼主: celestialgod (天)   2016-06-28 16:28:32
※ 引述《swedrf0112 (M)》之铭言:
: [问题类型]:程式咨询
: [软件熟悉度]: 入门
: [问题叙述]: magrittr 的 pipe 可以抓到最一开始的物件吗?
: [程式范例]:
: 其实原本的问题是,在问题中的 x ,有些时候是一些经过处理的物件,
: 给我现在遇到的例子 :
: # > head(season,20)
: # [1] "2000-01" "2001-02" "2002-03" "2003-04" "2004-05" "2005-06" "2006-07"
: "2007-08" "2008-09" "2009-10"
: # [11] "2010-11" "2011-12" "2012-13" "2013-14" "2014-15" "2015-16" "1996-97"
: "1997-98" "1998-99" "1999-00"
: 我想要针对 season 向量的 unique 进行排序,以一般的写法,我写成 :
: # > unique(season)
: # [1] "2000-01" "2001-02" "2002-03" "2003-04" "2004-05" "2005-06"
: # [7] "2006-07" "2007-08" "2008-09" "2009-10" "2010-11" "2011-12"
: # [13] "2012-13" "2013-14" "2014-15" "2015-16" "1996-97" "1997-98"
: # [19] "1998-99" "1999-00"
: unique(season)[order(as.numeric(substr(unique(season),1,4)))]
: 但是因为我觉得在里面 season 必须被 unique 两次,很浪费计算资源,
: 也可以先令一个 y=unique(season),把原本的式子 unique(season) 改成 y , 但这样
: 会浪费内存记这一个暂时的向量
: 所以我想用 magrittr 写,所以我才以刚刚这个简单的例子,想问是否可以呼叫 pipe 最
: 一开始的物件,因为我在 magrittr 的 help 里没找到这件事XD
: season %>% unique %>% substr(.,1,4) %>% as.numeric %>% order %>% "这边应该要呼
: 叫最一开始的 season"
: 针对一开始没把问题说清楚,跟 C 大与各位致歉,谢谢!
: 也请各位指正里面的想法是否有错误,或许根本就不会发生我想的事XDD
: [环境叙述]:
: [关键字]: Magrittr
测试一下存暂存变量的差异有多大
顺便看不同pipe的速度差异有多少
测试程式:
library(magrittr)
library(pipeR)
# data generation
# 生成一千万笔资料
dat <- seq.Date(as.Date("1990-01-01"),
as.Date("2015-10-01"), by = "quarter") %>>%
sample(1e7, TRUE) %>>% format("%Y-%m")
# 第一个方法,直接算,不用暂存变量
st <- proc.time()
res1 <- unique(dat)[order(as.numeric(substr(unique(dat),1,4)))]
proc.time() - st
# user system elapsed
# 0.20 0.05 0.25
# 第二个方法,用pipe跟block,暂存变量只会在block里面存活
st <- proc.time()
res2 <- dat %>% {
tmp1 <- unique(.)
tmp1 %>% substr(1,4) %>% as.numeric %>% order %>% {tmp1[.]}
}
proc.time() - st
# user system elapsed
# 0.12 0.07 0.19
print(length(ls(pattern = "tmp1")) > 0) # FALSE
# 同第二个,pipe改成%>>%
st <- proc.time()
res3 <- dat %>>% {
tmp2 <- unique(.)
tmp2 %>>% substr(1,4) %>>% as.numeric %>>% order %>>% (tmp2[.])
}
proc.time() - st
# user system elapsed
# 0.10 0.03 0.12
print(length(ls(pattern = "tmp")) > 0) # FALSE
# 用pipeR的assignment,把暂存变量存在外面
st <- proc.time()
res4 <- dat %>>% unique %>>% (~ tmp3 <- .) %>>% substr(1, 4) %>>%
as.numeric %>>% order %>>% (tmp3[.])
proc.time() - st
# user system elapsed
# 0.08 0.04 0.13
print(length(ls(pattern = "tmp3")) > 0) # TRUE
这样测一次好像pipeR + block表现最好,但是没有重复
接下来用加上重复在试一次:
library(microbenchmark)
microbenchmark(
res1 = unique(dat)[order(as.numeric(substr(unique(dat),1,4)))],
res2 = dat %>% {
tmp1 <- unique(.)
tmp1 %>% substr(1,4) %>% as.numeric %>% order %>% {tmp1[.]}
},
res3 = dat %>>% {
tmp2 <- unique(.)
tmp2 %>>% substr(1,4) %>>% as.numeric %>>% order %>>% (tmp2[.])
},
res4 = dat %>>% unique %>>% (~ tmp3 <- .) %>>% substr(1, 4) %>>%
as.numeric %>>% order %>>% (tmp3[.])
)
# Unit: milliseconds
# expr min lq mean median uq max neval
# res1 247.9882 289.0776 284.7083 291.4474 293.3180 429.9460 100
# res2 123.6998 124.9163 138.5741 126.2327 165.8535 170.4374 100
# res3 123.3566 124.9036 137.5461 125.9739 165.4394 171.2854 100
# res4 123.6355 124.9607 137.1532 125.9386 165.5996 170.9329 100
可以看出有暂存变量的表现都差不多
如果不想要存一个暂存变量在外面,就用block + pipe的方式吧
而且不同pipe在这里没什么差异
作者: swedrf0112 (M)   2016-06-28 16:46:00
感谢 C大!!
作者: MADNUG (1234567654321一下吧)   2016-06-30 22:25:00
推!

Links booklink

Contact Us: admin [ a t ] ucptt.com