@R语言学习
R语言中的data.table是一种数据处理时的高效率工具。在了解data.table之前先简单说明一下管道函数的使用方法。
常用的管道函数有以下三种:
%in%:表示包含于
%>%:表示向右传递
%$%:表示向右传递列,并可以直接按列操作
data = data%>%
as.data.table()
data = data%>%
as.data.table()%$%
.[,.(value = sum(value,na.rm = TRUE)),
by = c("period","index","area","id1","id2")]
data = data%$%
.[index%in%c("UV","DB","XS"),]
data = data%$%
.[index_name == "DB",index_name := "DB_PV"]%$%
.[index_name == "GZ",index_name := "GZ_UV"]%$%
.[index_name == "XSLZ",index_name := "XSLZ_UV"]
data = data %$%
.[,":="(id1 = substr(id1,1,6),
id2 = substr(id2,1,6))]%$%
.[,.(value = sum(value,na.rm = TRUE)),
by = c("period","index","area","id1","id2")]
data = data %$%
.[,.(period,id1,id2,area,index,value)]
N <- rank.P[ID2%in%target,.(ID2, Month, prov_ID, Province, Index_name,Value_adj_per)]
data = data%>%
setnames(c("period","car_id1","car_id2","area","value"), c("Month","ID1","ID2","Province","Value"))
data = data %>%
merge(data2, by.x="Province",by.y="Province",all.x = TRUE)%$%
.[,.(Month,Province,ID,index_name,ID2,Value)]
new_data <- data%>%
merge(data2,
by.x = c("ID1","ID2","Month","prov_ID","Province","Index_name"),
by.y = c("ID2","ID1","Month","prov_ID","Province","Index_name"),
suffixes = c("_P","_N"))
new_data <- new_data[, ":="(Score = (Value_adj_per_P+Value_adj_per_N)/2)]%$%
.[, Rank := row_number(-Score),
by = c("ID2","Month","prov_ID","Province","Index_name")]%>%
merge(phase, by = "Index_name")%$%
.[, Index_name := NULL]
11.复制某列某类别并重命名为另外一个类别
P.N<- copy(P.N)%$%
.[Phase == "留资",]%$%
.[Phase == "留资",Phase := "最终排行"]
P <- rank.P[ID2%in%target,
.SD[1:20],
by = c("ID2","Month","prov_ID","Province","Index_name")]%>%
na.omit()%$%
.[,.(ID2, Month, prov_ID, Province, Index_name,Value_adj)]