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