作者celestialgod (天)
看板R_Language
标题Re: [问题]资料处理速率缓慢
时间Sat Apr 29 20:33:17 2017
※ 引述《tan800630 (天ㄦ)》之铭言:
: [问题类型]:
:
: 效能谘询(我想让R 跑更快)
:
: [软体熟悉度]:
:
: 使用者(已经有用R 做过不少作品)
: [问题叙述]:
:
: 各位版上的前辈好,最近自己在玩脸书粉丝专页的资料,目前想要统计
: 某段时间的Po文中总共有哪些人按赞,每人的按赞次数,以及Po文时间,
: 目前已经先将粉丝专页(柯文哲 哈)某一段时间的po文都抓下来并存成RData档案,
: #直接抓getPost()的资料存进去
: 目前希望将档案整理成 "ID","最早按赞文章日期","最後按赞文章日期","总共按赞次数"
: 的格式,目前使用的方式仍然是用回圈逐次读取每一个档案并且记录按赞者的
: 相关内容(我知道回圈效率很低O_Q 自己尝试使用apply系列但失败)
: 然而由於按赞者众多,目前照着我预设的方式会跑非常久,因此想请教各位有没有
: 甚麽建议可以让整个程式的处理效率更快速
: 再麻烦各位前辈指教~~~~~
:
: [程式范例]:
:
: 程式码
: https://pastebin.com/e9WY2AjD
: 范例档案下载处(放了三篇文章的档案,请参考)
: http://doora.qiniudn.com/lH2Z7.rar
:
: [环境叙述]:
:
: R version 3.3.2 (2016-10-31)
: Platform: x86_64-w64-mingw32/x64 (64-bit)
: Running under: Windows 8.1 x64 (build 9600)
: locale:
: [1] LC_COLLATE=Chinese (Traditional)_Taiwan.950
: [2] LC_CTYPE=Chinese (Traditional)_Taiwan.950
: [3] LC_MONETARY=Chinese (Traditional)_Taiwan.950
: [4] LC_NUMERIC=C
: [5] LC_TIME=Chinese (Traditional)_Taiwan.950
: attached base packages:
: [1] stats graphics grDevices utils datasets methods base
: other attached packages:
: [1] Rfacebook_0.6.12 httpuv_1.3.3 rjson_0.2.15 httr_1.2.1
: loaded via a namespace (and not attached):
: [1] R6_2.2.0 tools_3.3.2 Rcpp_0.12.9
:
: [关键字]:
:
: 回圈
後面有R code,不过我先说,我建议这种资料直接存到资料库
一个表存POST,一个表存Like,一个表存Comment
每一个POST都存一个POST ID,Like跟Comment表再存的时候也要记得给一个post id
然後用SQL就可以轻松串出你要的结果了:
select b.from_id user_id, min(a.post_time) first_like,
max(a.post_time) last_like, count(a.post_time) count_like
from post_table a, like_table b
where 1=1
and a.post_id = b.post_id
group by b.from_id
但是要用R做的话大概是这样:
library(pipeR)
library(data.table)
library(lubridate)
postDT <- lapply(list.files("lH2Z7", full.names = TRUE), function(fn){
load(fn)
data.table(post_time = parse_date_time(substring(post$post$created_time,
1, 19), "ymd HMS"), post$likes)
}) %>>% rbindlist
outDT <- list(
dcast(postDT, from_id + from_name ~ 1, min, value.var = "post_time") %>>%
setnames(".", "first"),
dcast(postDT, from_id + from_name ~ 1, max, value.var = "post_time") %>>%
setnames(".", "last"),
dcast(postDT, from_id + from_name ~ 1, length, value.var = "post_time") %>>%
setnames(".", "count")
) %>>%
Reduce(f = function(x, y) merge(x, y, by = c("from_id", "from_name")))
# 结果:
# from_id from_name first last count
# 1: 1000352350101347 王相现 2017-04-11 12:06:46 2017-04-13 14:31:01 3
# 2: 1000365193433309 Erica Wang 2017-04-13 14:31:01 2017-04-13 14:31:01 1
# 3: 1000465496719965 陈健男 2017-04-12 11:00:15 2017-04-12 11:00:15 1
# 4: 1001166286680917 Jolie Liaw 2017-04-12 11:00:15 2017-04-12 11:00:15 1
# 5: 1001188363348018 翁茂翔 2017-04-12 11:00:15 2017-04-13 14:31:01 2
dcast一定要吃到global environment宣告的函数(摊手
所以我就乖乖写了,没有用lapply配合assign宣告到.GlobalEnv
--
R资料整理套件系列文:
magrittr #1LhSWhpH (R_Language) https://goo.gl/72l1m9
data.table #1LhW7Tvj (R_Language) https://goo.gl/PZa6Ue
dplyr(上.下) #1LhpJCfB,#1Lhw8b-s (R_Language) https://goo.gl/I5xX9b
tidyr #1Liqls1R (R_Language) https://goo.gl/i7yzAz
pipeR #1NXESRm5 (R_Language) https://goo.gl/zRUISx
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.38.134.123
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1493469201.A.557.html
1F:推 tan800630: 谢谢C大! 晚点试试看 04/30 20:14
2F:推 tan800630: 其实dplyr之前也摸过,但感觉还没内化成第一时间会想到 05/01 10:58
3F:→ tan800630: 处理效率自己实在是不大会比较,看来之後还是要多观摩 05/01 10:59
我这里是用data.table (摊手
tidyr也可做到一样的效果
※ 编辑: celestialgod (114.38.134.123), 05/01/2017 15:44:50