首页 > 解决方案 > 计算给定时间空间范围内数据的百分比

问题描述

我有一个这样的数据库,但更大。

  Indicea<-c(1,2,3,5,3,1,3,5,3,6,NA,2,1,1,3,2)
  Indiceb<-c(12,15,12,14,13,16,14,13,15,12,14,13,NA,13,11,12)
  Indicec<-c(100,NA,120,154,125,201,102,150,102,105,140,156,118,113,175,189)
  Especialidad<-c("gato","gato","gato","perro","perro","perro","perro",
        "buho","buho","buho","buho","tigre","tigre","tigre",NA,"tigre")
  Indiced<-c(0.1,0.5,06,032,0.1,0.25,0.23,0.12,0.15,NA,0.25,0.45,1.0,0.5,0.26,0.45)
  Fecha<-c("01/03/2020","02/03/2020","03/03/2020","04/03/2020",
     "05/03/2020","06/03/2020","07/03/2020","08/03/2020",
     "09/03/2020","10/03/2020","11/03/2020","12/03/2020",
     "13/03/2020","14/03/2020","15/03/2020","15/03/2020")
 data<-dataframe(Indicea, Indiceb,Indicec,Indiced,Especialidad,Fecha)

我的用户界面很完美这是我发球的一小部分,我有一个可以根据列改变的滑块输入。我需要做一个按“Especialidad”分组的表格,并计算sliderInput中所选数字出现在范围内的次数及其代表的百分比

   output$Rango<-renderUI({
req(input$SeleccioneIndice)
minn <- min(BD9.3()[,input$SeleccioneIndice], na.rm = TRUE)
maxx <- max(BD9.3()[,input$SeleccioneIndice], na.rm = TRUE)
sliderInput("Rango",label = "Seleccione un rango", min = minn, 
max=maxx, value=c(minn,maxx))
 })
BD9.5<-reactive({
BD9.4<-BD9.3()
BD9.4$Intervalo<-NA
BD9.4$Intervalo<-replace(BD9.4$Intervalo,BD9.4[,which
(names(BD9.4)==input$SeleccioneIndice)]>=as.numeric(minn),
"Correcto")
BD9.4$Intervalo<-replace(BD9.4$Intervalo,BD9.4[,which
(names(BD9.4)==input$SeleccioneIndice)]<=as.numeric(maxx), 
 "Fallo") 
BD9.4$Intervalo<-replace(BD9.4$Intervalo,BD9.4[,which
(names(BD9.4)==input$SeleccioneIndice )]<=as.numeric(minn),  
"Correcto")
BD9.4$Intervalo<-replace(BD9.4$Intervalo,BD9.4[,which
(names(BD9.4)==input$SeleccioneIndice)]>=as.numeric(maxx),
"Fallo")


BD9.6<-as.data.frame(table(BD9.4$specialty.name,BD9.4$Intervalo))
names(BDa4)<-c("Especialidad","Intervalo","Total")

BD9.6$Porcentaje<-NA

for(i in levels(factor(BD9.6$Especialidad))){
  
BD9.6[BD9.6$Especialidad==i,]$Porcentaje=round((
 BD9.6[BD9.6$Especialidad==i,]$Total/sum
 (BD9.6[BD9.6$Especialidad==i,]$Total))*100,2)
}

BD9.6 <- arrange(BD9.6, Especialidad, Cumplimiento) 
BD9.6


output$summary9<-renderPrint({
  BD9.6%>%
  filtro<-subset(BD9.6, Cumplimiento == "Correcto")
  print(arrange(filtro,-Porcentaje))
})

标签: rshiny

解决方案


推荐阅读