Re: [問題] 想利用data.table將Rawdata切割欄位

作者: sacidoO (阿罵)   2015-03-02 05:59:25
無聊把c大的程式改用另外一個方式寫一下
想請問c大怎麼推估您程式分割400萬row需要67秒?
我看您分割40萬資料的elased time只需要3.5秒的說....
以下是我的code...其中數據產生部分是引用c大的方法
#generate dat as original data set
library(data.table)
dat = fread("001female 2019920404\n002male 3019920505\n003male
4019920606\n004female 5019920707\n", sep="\n", sep2="", header=FALSE)
#split by " "
library(reshape2)
dat<-colsplit(dat[[1]], " ", c("temp1", "temp2"))
#seperate column by variable length
data<-data.frame( id=substr(dat[,1],1,3), sex=substr(dat[,1],4,9),
income=as.numeric(substr(dat[,2],1,2)),
birthday=as.numeric(substr(dat[,2],3,10)) )
#generate 400000 data
dat = fread(paste0(rep("001female 2019920404\n002male 3019920505\n003male
4019920606\n004female 5019920707\n", 100000), collapse=""),sep="\n", sep2="",
header=FALSE)
#system start time
tt = proc.time()
dat<-colsplit(dat[[1]], " ", c("temp1", "temp2"))
data<-data.frame( id=substr(dat[,1],1,3), sex=substr(dat[,1],4,9),
income=as.numeric(substr(dat[,2],1,2)),
birthday=as.numeric(substr(dat[,2],3,10)) )
proc.time() - tt
#user system elapsed
#36.97 0.07 37.11
我的程式碼沒看錯方法的話應該是跑了37.11秒....
CPU Intel i7-3770 @3.40GHz Win7 64bit with R 3.0.1
※ 引述《celestialgod (攸藍)》之銘言:
: > [問題敘述]:
: > 各位前輩們好,
: > 小弟有一筆資料大約為500MB左右,
: > 因這筆資料為原始檔案,
: > 還沒將欄位定義好,
: > 所以小弟想詢問如何讀入data.table後,
: > 在將欄位進行切割?
: > example:
: > var1
: > 1 001female 2019920404
: > 2 002male 3019920505
: > 3 003male 4019920606
: > 4 004female 5019920707
: > 希望可以透過欄位切割後變成:
: > id sex income birthday
: > 1 001 female 20 19920404
: > 2 002 male 30 19920505
: > 3 003 male 40 19920606
: > 4 004 female 50 19920707
: 手癢寫了一下@@
: code:
: library(data.table)
: library(magrittr)
: dat = fread("001female2019920404\n002male 3019920505\n003male
: 4019920606\n004female5019920707\n", sep="\n", sep2="", header=FALSE)
: library(Rcpp)
: library(inline)
: sourceCpp(code = '
: #include <Rcpp.h>
: using namespace Rcpp;
: // [[Rcpp::export]]
: List dat_split_f( std::vector< std::string > strings, NumericVector loc) {
: int loc_len = loc.size(), num_strings = strings.size();
: List output(num_strings);
: for( int j=0; j < num_strings; j++ )
: {
: std::vector< std::string > tmp;
: for (int i=0; i < loc_len-1; i++)
: tmp.push_back( strings[j].substr(loc[i], loc[i+1] - loc[i]) );
: output[j] = tmp;
: }
: return output;
: }')
: # 第二個input 第一個固定是0 第二個是第一個字串結束位置
: # 第三個是第二個字串結束位置,依此類推,有k個column放k+1個值
: # 這個可以用手算,也可以用regular expression去找對應位置,自行定奪
: # 補充: 用C++邏輯想是,前k個是每個字串開始位置,最後一個是總長度
: dat_split = dat_split_f(dat[[1]], c(0, 3, 9, 11, 19)) %>%
: do.call("rbind", .) %>% data.table()
: # 之後自行再把需要轉成數字的chr轉成numeric就好
: 測試一下40萬個row
: dat = fread(paste0(rep("001female2019920404\n002male 3019920505\n003male
: 4019920606\n004female5019920707\n", 100000), collapse=""),
: sep="\n", sep2="", header=FALSE)
: tt = proc.time()
: dat_split = dat_split_f(dat[[1]], c(0, 3, 9, 11, 19)) %>%
: do.call("rbind", .) %>% data.table()
: proc.time() - tt
: # user system elapsed
: # 3.34 0.01 3.50
: 四百萬個ROW大概是67秒
: CPU: Intel Celeron B830@1.80G in windows 7 64bit with R 3.1.2
: 還要更快可以找我的文章(R_Language #1JiQHkrP),在Rcpp中加入openmp加速
※ 編輯: sacidoO (128.196.43.84), 03/02/2015 06:09:07
作者: celestialgod (天)   2015-03-02 10:06:00
不是推估,是實際跑的結果謝謝您的分享,R真的在切割字串上慢不少,CPU的等級差距不少還差到十倍.....其實時間會往上疊加是因為用了rbind,我稍微修改一下,應該可以更快

Links booklink

Contact Us: admin [ a t ] ucptt.com