R collapsibleTree:属性的加权均值

时间:2018-06-26 13:04:17

标签: r

我创建了一个简单的层次树:

library (collapsibleTree)
SBU <- c("ASG","ASG","ASG","CS","CS","CS","CS")
Site <- c("China","Chula","Foley","Monroe","Miramar","Phoenix","Rockford")
OTD <- c(0.9, 0.52, 0.60, 0.85, 0.98, 0.92, 0.32)
Shipments <- c(100, 200, 300, 400, 150, 75, 90)

Network <- data.frame(SBU,Site,OTD,Shipments)

collapsibleTreeSummary(df = Network,hierarchy = c("SBU","Site"), attribute = "OTD")

但我希望发生以下情况:

1)对于不同的节点,它当前对属性进行汇总。我希望将其作为使用运货的OTD的加权平均值。 即

  • ASG节点应为.62(.9 * 100 + .52 * 200 + .6 * 300)/(100 + 200 + 300)
  • CS节点应为.82
  • 网络将为.72

2)颜色中断取决于属性(OTD> = .95 =绿色,OTD <.95或OTD> =。90 =黄色,OTD <.90 =红色)

3)节点大小取决于货件大小

更新

我已经找出了#2和#3,但是我仍然不知道#1。它们是collapsibleTree中一个称为aggFun的参数,它允许mean,但我需要它来做weighted mean。有什么想法吗?

library (collapsibleTree)

rm(list=ls())

SBU <- c("ASG","ASG","ASG","CS","CS","CS","CS")
Site <- c("China","Chula","Foley","Monroe","Miramar","Phoenix","Rockford")
OTD <- c(0.9, 0.52, 0.60, 0.85, 0.98, 0.92, 0.32)
Shipments <- c(100, 200, 300, 400, 150, 75, 90)

Network <- data.frame(SBU,Site,OTD,Shipments)

# Add in colors
NetworkColor <- ifelse(weighted.mean(Network$OTD,Network$Shipments) >= .95, "green", ifelse(weighted.mean(Network$OTD,Network$Shipments) < .90, "red", "yellow"))

SBUColor <- Network %>%
  group_by(SBU) %>%
  summarize(Criteria = weighted.mean(OTD, Shipments,na.rm=TRUE)) %>%
  mutate(Colors = ifelse(Criteria >= .95, "green", ifelse(Criteria < .90, "red", "yellow")))

SiteColor <- Network %>%
  group_by(SBU, Site) %>%
  summarize(Criteria = mean(OTD,na.rm=TRUE)) %>%
  mutate(Colors = ifelse(Criteria >= .95, "green", ifelse(Criteria < .90, "red", "yellow")))

# Create Tree
collapsibleTree(df = Network,hierarchy = c("SBU","Site"), attribute = "OTD", aggFun = weighted.mean, tooltip = TRUE, nodeSize = "Shipments",   
                fill = c(NetworkColor,SBUColor$Colors,SiteColor$Colors))

0 个答案:

没有答案