如何为R中的Sankey图准备数据

时间:2017-07-18 15:11:18

标签: r sankey-diagram

我想从我的数据中创建一个Sankey图。 我这样做如下:

#Create the data
proc<-sample(c("EMR","RFA","Biopsies"), 100, replace = TRUE)
#Sample dates
dat<-sample(seq(as.Date('2013/01/01'), as.Date('2017/05/01'), by="day"), 100)
#Generate 20 random ID's in no particular order:
HospNum_Id<-sample(c("P433224","P633443","K522332","G244224","S553322","D0739033","U873352","P223333","Y763634","I927282","P223311","P029834","U22415","U234252","S141141","O349253","T622722","J322909","F630230","T432452"), 100, replace = TRUE)
df<-data.frame(proc,dat,HospNum_Id)

 #Organize the flow of procedures:
    library(data.table)
    library(dplyr)
    library(googleVis)
    Sankey<-dcast(setDT(df)[, if(any(proc=="EMR"|proc=="RFA")) .SD, HospNum_Id], HospNum_Id~rowid(HospNum_Id), value.var ="proc")
    PtFlow<-Sankey
    PtFlow<-data.frame(PtFlow)
    names(PtFlow)<-c("ord1","ord2","ord3","ord4","ord5","ord6","ord7","ord8","ord9","ord10","ord11","ord12")

   orders <- PtFlow %>%
  select(ord1, ord2, ord3, ord4, ord5,ord6,ord7,ord8,ord9,ord10,ord11,ord12)

#Create another data frame for the function
orders.plot <- data.frame()
data.frame(from= character(0), to= character(0), n = numeric(0))

到目前为止,此工作正常。但是我遇到以下问题:

for (i in 2:ncol(orders)) {

  ord.cache <- orders %>%
    group_by(orders[ , i-1], orders[ , i]) %>%
    summarise(n=n())
  colnames(ord.cache)[1:2] <- c('from', 'to')

  # adding tags to carts
  ord.cache$from <- paste(ord.cache$from, '(', i-1, ')', sep='')
  ord.cache$to <- paste(ord.cache$to, '(', i, ')', sep='')
  orders.plot <- rbind(orders.plot, ord.cache)
}

当我进入该功能时,RStudio总是崩溃,我甚至无法调试错误。如果我一点一点地运行循环,我认为错误是在rbind,但我无法确定。

0 个答案:

没有答案