首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据集的分层过滤

数据集的分层过滤
EN

Stack Overflow用户
提问于 2021-04-23 02:23:40
回答 1查看 43关注 0票数 0

我想根据几个标准(主要是疾病类型和位置)来过滤我的数据集。我已经决定,对于某些部位,没有特定类型的疾病(胰腺疾病只能是一种感染-这就是为什么在Organ=Pancreas -时选择made Total。我知道这是不现实的,这只是一个假设)。所以我想做分层过滤(变量Organ在层次结构的顶部,TypeOfDisease在层次结构的底部)。我正在寻找一种最短的方法(一种递归的if语句)来计算if语句,而不是将所有类别两两组合在一起。

代码语言:javascript
复制
library(shiny)
library(tidyverse)
TypeOfDisease<-c(rep("Infection",12),rep("Cancer",5),rep("Infection",14),
                 rep("Cancer",9),rep("Infection",8),rep("Cancer",7),rep("Infection",15),rep("Cancer",0),
                 rep("Infection",12),rep("Cancer",18))
Organ<-c(rep("Oesophage",17),rep("Stomach",23),rep("Lung",15),rep("Pancreas",15),rep("Liver",30))
data<-data.frame(TypeOfDisease,Organ)
addmargins(table(data$TypeOfDisease,dataF$Organ))

ui<-fluidPage(
  
  sidebarLayout(
    sidebarPanel(
      selectInput("Organ","Select the organ",
                  choices = c("Total",levels(data$Organ))),
      
      uiOutput("ui"),
    ),
    mainPanel(
      
      fluidRow(
        column(5,tableOutput("table")),
        column(7,
               
               fluidRow(verbatimTextOutput("matrix")),
               fluidRow(verbatimTextOutput("Nrow"))
               )
      )
      
      
      
      )
  )
  
)


server<-function(input,output){
  
  
  output$ui<-renderUI(
    
    switch (input$Organ,
            
      "Total" = selectInput("TypeOfDis","Type of disease",
                                      choices = c("Total","Infection","Cancer")),
      "Oesophage" = selectInput("TypeOfDis","Type of disease",
                                choices = c("Total","Infection","Cancer")),
      "Stomach" = selectInput("TypeOfDis","Type of disease",
                                choices = c("Total","Infection","Cancer")),
      "Lung" = selectInput("TypeOfDis","Type of disease",
                                choices = c("Total","Infection","Cancer")),
      "Pancreas" = selectInput("TypeOfDis","Type of disease",
                                choices = c("Total")),
      "Liver" = selectInput("TypeOfDis","Type of disease",
                                choices = c("Total","Infection","Cancer")),
    )
  )
  
  dataFilter<-reactive({
    
    if(input$TypeOfDis=="Total"&input$Organ=="Total"){
      data
    }else if(input$TypeOfDis=="Total"&input$Organ=="Oesophage"){
        data%>%filter(Organ=="Oesophage")
    }else if(input$TypeOfDis=="Total"&input$Organ=="Stomach"){
       data%>%filter(Organ=="Stomach")
    }else if(input$TypeOfDis=="Total"&input$Organ=="Lung"){
      data%>%filter(Organ=="Lung")
    }else if(input$TypeOfDis=="Total"&input$Organ=="Pancreas"){
      data%>%filter(Organ=="Pancreas")
    }else if(input$TypeOfDis=="Total"&input$Organ=="Liver"){
      data%>%filter(Organ=="Liver")
    }else if(input$TypeOfDis=="Infection"&input$Organ=="Total"){
      data%>%filter(TypeOfDisease=="Infection")
    }else if(input$TypeOfDis=="Infection"&input$Organ=="Oesophage"){
      data%>%filter(TypeOfDisease=="Infection"&Organ=="Oesophage")
    }else if(input$TypeOfDis=="Infection"&input$Organ=="Stomach"){
      data%>%filter(TypeOfDisease=="Infection"&Organ=="Stomach")
    }else if(input$TypeOfDis=="Infection"&input$Organ=="Lung"){
      data%>%filter(TypeOfDisease=="Infection"&Organ=="Lung")
    }else if(input$TypeOfDis=="Infection"&input$Organ=="Pancreas"){
      data%>%filter(TypeOfDisease=="Infection"&Organ=="Pancreas")
    }else if(input$TypeOfDis=="Infection"&input$Organ=="Liver"){
      data%>%filter(TypeOfDisease=="Infection"&Organ=="Liver")
    }else if(input$TypeOfDis=="Cancer"&input$Organ=="Oesophage"){
      data%>%filter(TypeOfDisease=="Cancer"&Organ=="Oesophage")
    }else if(input$TypeOfDis=="Cancer"&input$Organ=="Stomach"){
      data%>%filter(TypeOfDisease=="Cancer"&Organ=="Stomach")
    }else if(input$TypeOfDis=="Cancer"&input$Organ=="Lung"){
      data%>%filter(TypeOfDisease=="Cancer"&Organ=="Lung")
    }else if(input$TypeOfDis=="Cancer"&input$Organ=="Pancreas"){
      data%>%filter(TypeOfDisease=="Cancer"&Organ=="Pancreas")
    }else if(input$TypeOfDis=="Cancer"&input$Organ=="Liver"){
      data%>%filter(TypeOfDisease=="Cancer"&Organ=="Liver")
      }else{(data%>%data%>%filter(TypeOfDisease=="Cancer"))}
        
        
  })
  output$table<-renderTable ({dataFilter()})
  output$matrix<-renderPrint({addmargins(table(data$TypeOfDisease,dataF$Organ))})
  output$Nrow<-renderPrint({nrow(dataFilter())})
  
}



shinyApp(ui,server)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-23 03:26:26

也许你可以像这样过滤

代码语言:javascript
复制
  dataFilter2<-reactive({
    
    if(input$TypeOfDis=="Total"&input$Organ=="Total"){
      data
    }else {
      if(input$TypeOfDis=="Total"){
        data[data$Organ==input$Organ,]
      }else data[data$TypeOfDisease==input$TypeOfDis & data$Organ==input$Organ ,]
    } 
    
  })
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67218854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档