R实现基本的rank划分

浏览: 1337

实现目的是数据进行区间划分(<_0_>)

#Code:
library(DBI)
library(ROracle)
drv=dbDriver('Oracle')
conn=dbConnect(drv,'test','test','192.100.10.10:1521/ZNJT')
# get the flux of the current time (hour)
rs=dbSendQuery(conn,"select * from dw_fact_flux where gcsj=to_char(sysdate,'yyyymmdd')-30000+900+8 
 and sd=to_char(sysdate,'hh24')")
data=fetch(rs)
data=as.matrix(data)
#get the count,mean,min,max,threshold,rank
rownum=nrow(data)
minval=min( as.numeric(data[,12]))
meanval= mean(as.numeric(data[,12]))
maxval=max( as.numeric(data[,12]))
threshold1=(minval+meanval)/2
threshold2=meanval
threshold3=(meanval+maxval)/2
# According to the scope of grouping
rankval=cut(as.numeric(data[,12]),c(minval-1,threshold1,meanval,threshold3,maxval),labels = c("1","2","3","4"))

datae=as.data.frame(cbind(data,rankval)) 
dbWriteTable(conn,'DW_ACD_HDZT',datae, row.names = F, append = TRUE)
dbDisconnect(conn)

注意:1、datae=as.data.frame(cbind(data,rankval)) 如何换成datae=as.data.frame(cbind(data,RANKVAL))则会出现顺序混乱

2、表名要使用大写,datae转换as.data.frame

推荐 0
本文由 华青莲 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册