目录
1、data.table包中特殊符号.SD(通过.SDcols选择的变量作处理)、.SDcols(变量列选择)、.I(返回位置)、.N(计数)、.BY、J、CJ、SJ、:=
5、row_number、with_groups与order、by综合比较:
6、字段拆分函数tstrsplit与str_split_fixed比较:
1、data.table包中特殊符号.SD(通过.SDcols选择的变量作处理)、.SDcols(变量列选择)、.I(返回位置)、.N(计数)、.BY、J、CJ、SJ、:=
2、分组计数(单一方法)比较:
sys_time_print(data_test%>% group_by(var) %>% tally())
#运行耗时:
[1] "Finished in 0.870s elapsed (0.670s cpu)"
sys_time_print(data_test[,.N,by=var])
#运行耗时:
[1] "Finished in 0.420s elapsed (0.560s cpu)"
3、多分组情况下比较 :
sys_time_print(test <- data_test[,var1_tag:=as_date(var1)]
[,.(var3_sum=sum(var3)),by="class1,id,var1_tag,var2"])
#运行耗时:
[1] "Finished in 2.500s elapsed (4.730s cpu)"
sys_time_print(test <- data_test[,var1_tag:=as_date(var1)]
[,.(var3_sum=sum(.SD)),by="class1,id,var1_tag,var2",.SDcol="var3"])
#运行耗时:耗时较长
sys_time_print(test <- data_test%>%
mutate(var1_tag=as_date(var1))%>%
group_by(class1,id,var1_tag,var2) %>%
summarise(var3_sum=sum(var3)))
#运行耗时:
[1] "Finished in 10.0s elapsed (9.690s cpu)"
4、if_else和fifelse函数比较:
fifelse()函数,可对照dplyr包if_else、软件内置ifelse函数计算效率
#dplyr包if_else函数
#对数据进行多变量判断、添加标签(数据量20w+)
data_test%>%
mutate(final_tag=if_else(s1_rank==1,"tag1",
if_else(s2_rank==1,"tag2",
if_else(s3_rank==1,"tag3",
if_else(s4_rank==0,"no_tag","tag4")))))
#data.table包fifelse函数
data_test%>%
mutate(final_tag=fiflese(s1_rank==1,"tag1",
fiflese(s2_rank==1,"tag2",
fiflese(s3_rank==1,"tag3",
fiflese(s4_rank==0,"no_tag","tag4")))))
![](https://i-blog.csdnimg.cn/blog_migrate/998e5a78d3e728b0aac52f0286405e9c.png)
![](https://i-blog.csdnimg.cn/blog_migrate/74b6db420f30476c9ddfe36e3202be7f.png)