【问题标题】:mean and sd with user specified conditions具有用户指定条件的均值和标准差
【发布时间】:2019-03-23 15:23:45
【问题描述】:

我已经转换成以下格式:

Date         price Industry  stock
29/10/2018   3      Airline   A
28/10/2018   4      Airline   A
27/10/2018   2      Airline   A
29/10/2018   5      Bank      B
29/10/2018   3      Food      C
28/10/2018   4      Bank      B
27/10/2018   2      Bank      B
27/10/2018   6      Food      C

我还输入了 Start Date 、 end date 、行业和库存。我使用以下代码根据用户输入创建了一个子集:

desc_filtered <- reactive({
c<-  dailyprice_gather %>%
  group_by(stocks) %>%
  mutate(
price_at_date = price[Date == selected_date2],
new_price = price - price_at_date)
c <- subset(c, Date>=input$dateRange[1] )
c <- subset(c, Date<=input$dateRange[2] )
c <- subset(c, Industry == input$industry2)
c <- subset(c, stocks == input$equities)
 })

我想显示指定时间段内用户选择的行业和股票的平均值和标准差。行业和股票是多项选择下拉 我可能需要使用 rowMeans 但不确定如何将 rowMean 用于反应函数。

【问题讨论】:

  • c 是 R 中的一个函数。我认为将c 用作对象不是一个好主意,这会造成很多混乱。

标签: r shiny


【解决方案1】:

dplyr 包中,您可以使用函数 filter()group_by() 来计算股票的平均价格(和标准差)。

假设您的数据集是df

df %>%
   filter(Date >= input$dateRange[1] & Date <= input$dateRange[2]) %>%
   filter(Industry %in% input$industry_choices) %>%
   group_by(Industry, stock) %>%
   summarise(
       price_mean = mean(price),
       price_sd   = mean(sd)
   )

在 Shiny 中,输入被视为字符串,有时您需要使用 group_by_()。 例如,如果用户可以选择要分组的变量:group_by_(input$grouping_choice)

【讨论】:

  • 感谢您的更新。如何在屏幕上显示汇总数据? output$summ&lt;-renderPrint({ dailyprice_gather %&gt;% filter(Date &gt;= input$dateRange[1] &amp; Date &lt;= input$dateRange[2]) %&gt;% filter(Industry %in% input$industry2) %&gt;% group_by(Industry, stocks) %&gt;% summarise( price_mean = mean(price), price_sd = mean(sd) ) print(price_mean) }) 然后在 Ui 中使用 verbatimTextOutput(outputId = "summ"),。但它给 Print(price_mean) 的错误
  • 您的操作结果是一个数据框。有几种打印数据框的方法,一个很好的方法是使用库 DT。在您的 server 中,使用 renderDataTable() 而不是 renderPrint() 来调用您的响应式并将结果转换为数据表对象,并在您的 ui 部分中使用 datatableOutput()
【解决方案2】:

这是一个基于您在问题中描述的工作示例。我认为你的方向是正确的。关键是为数据框的子集创建一个反应对象。在我的示例中,它被称为sub_dat。然后我们可以根据sub_dat计算meansd,并用textOutput打印出来。

由于您使用的是dplyr,我认为没有必要使用基本 R 子集函数。我们可以使用filter 完成所有子集任务。另一件事是我认为您不需要任何 group_by 操作。但如果你这样做,很容易修改我的示例以包含group_by 操作。

# Load packages
library(tidyverse)
library(lubridate)
library(shiny)

# Create example data frame
dailyprice_gather <- tribble(
  ~Date,   ~price, ~Industry,  ~stock,
'29/10/2018',   3,      'Airline',   'A',
'28/10/2018',   4,      'Airline',   'A',
'27/10/2018',   2,      'Airline',   'A',
'29/10/2018',   5,      'Bank',      'B',
'29/10/2018',   3,      'Food',      'C',
'28/10/2018',   4,      'Bank',      'B',
'27/10/2018',   2,      'Bank',      'B',
'27/10/2018',   6,      'Food',      'C')

# Convert to date class
dailyprice_gather <- dailyprice_gather %>% mutate(Date = dmy(Date))

# A vector to show the choices for industry
ind_choices <- sort(unique(dailyprice_gather$Industry))

# A vector to show the choices for the stock
stock_choices <- sort(unique(dailyprice_gather$stock))

# Create the UI
ui <- fluidPage(
  # Select the date range
  dateRangeInput(inputId = "DateRange", label = "Select Date Range", 
                 start = min(dailyprice_gather$Date), 
                 end = max(dailyprice_gather$Date),
                 min = min(dailyprice_gather$Date),
                 max = max(dailyprice_gather$Date)),
  # Select the Industry
  selectInput(inputId = "Industry", label = "Select the Industry",
              choices = ind_choices, selected = ind_choices[1]),
  # Select the stock
  selectInput(inputId = "Stock", label = "Select the Stock",
              choices = stock_choices, selected = stock_choices[1]),
  # Show the mean
  h3("The Mean of Price"),
  textOutput(outputId = "MEAN"),
  # Show the standard deviation
  h3("The SD of Price"),
  textOutput(outputId = "SD")
)

# Create SERVER
server <- function(input, output) {
  # # Create a reactive object for subset data frame
  sub_dat <- reactive({
    dailyprice_gather %>%
      filter(Date >= input$DateRange[1], 
             Date <= input$DateRange[2],
             Industry %in% input$Industry,
             stock %in% input$Stock)
  })
  # Calculate the mean and sd based on sub_dat
  output$MEAN <- renderText({
    as.character(mean(sub_dat()$price))
  })
  output$SD <- renderText({
    as.character(sd(sub_dat()$price))
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

【讨论】:

  • 感谢您的回答。但我收到以下错误。 “结果的长度必须为 91816,而不是 0”。有什么想法吗?
  • 您似乎正在测试您的真实数据集。由于我没有您的真实数据集,因此我不知道发生了什么。请确保代码适用于您的示例数据框,然后尝试找出您的示例数据框与实际数据集之间的差异。
猜你喜欢
  • 1970-01-01
  • 2014-09-04
  • 2020-11-14
  • 2018-08-16
  • 1970-01-01
  • 2021-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多