【问题标题】:Function for writing an automated report in R在 R 中编写自动报告的功能
【发布时间】:2022-01-05 05:52:35
【问题描述】:

所以我正在尝试使用 R 函数编写一个自动报告。我试图回答的问题之一是“在本月的第一周,查看次数最多的 10 个产品是什么?在表格中显示结果,其中包含产品的标识符、类别和查看次数。” .为此,我编写了以下函数

    most_viewed_products_per_week <- function (month,first_seven_days, views){
      month <- views....February.2020.2
    
      first_seven_days <- function( month, date_1, date_2){
        date_1 <-2020-02-01
        date_2 <- 2020-02-07
          return (first_seven_days)}
    
      views <-function(views, desc){
        return (views.head(10))}
        }
    print(most_viewed_products_per_week)

但是我得到的输出是这样的:

function (month,first_seven_days, views){
  month <- views....February.2020.2

  first_seven_days <- function( month, date_1, date_2){
    date_1 <-2020-02-01
    date_2 <- 2020-02-07
      return (first_seven_days)}

  views <-function(views, desc){
    return (views.head(10))}

我该如何解决这个问题? 这份报告有更多这样的问题,所以我试图从一开始就让我的函数编写尽可能正确。

提前致谢, 江户

【问题讨论】:

  • 拜托,您能否提供您的数据样本,以便我们在我们的机器上复制它?您可以使用dput(head(your_data)) 复制输出。谢谢。
  • 你的函数没有意义。如果 date_1date_2 在函数中被重新定义为常量,为什么它们是 first_seven_days 的参数? (如date_1 &lt;-2020-02-01)。其他问题:从未使用过month 参数; views 函数本身就是一个函数(views(views.....));views.head() 不存在 - 看起来像是一个执行不佳的类似 python 的代码
  • 我还建议您阅读游览和基本堆栈溢出说明。你可以从stackoverflow.com/help/how-to-ask开始

标签: r function


【解决方案1】:

在函数中编写代码是一种很好的做法。我仍然建议你让你的代码做你想做的事,然后考虑你想把哪些部分包装在一个函数中(以供将来重用)。这是为了让你继续前进。

一般来说:为了支持您的分析,请确保您的数据属于正确的类别。 IE。日期被格式化为日期,数字为双精度或整数等。这将使您可以访问许多帮助函数和包。

对于手头的案例,请阅读 {tidyverse},尤其是 {dplyr},它可以帮助您编写代码 pipes

模拟数据

如前所述 - 如果您提供可重现的示例,您会在 Stackoverflow 上找到很多朋友。
您的问题表明您的数据看起来有点像以下模拟数据。
酌情调整(或提供示例)

library(tibble)    # tibble are modern data frames
library(dplyr)     # for crunching tibbles/data frames
library(lubridate) # tidyverse package for date (and time) handling

df <- tribble(     # create row-tibble
    ~date, ~identifier, ~category, ~views
    ,"2020-02-01", 1, "TV", 27
    ,"2020-02-02", 2, "PC", 40
    ,"2020-02-03", 1, "TV", 12
    ,"2020-02-03", 2, "PC", 2
    ,"2020-02-08", 3, "UV", 200
  ) %>% 
  mutate(date = ymd(date))    # date is read in a character - lubridate::ymd() for date

这会产生

> df
# A tibble: 5 x 4
  date       identifier category views
  <date>          <dbl> <chr>    <dbl>
1 2020-02-01          1 TV          27
2 2020-02-02          2 PC          40
3 2020-02-03          1 TV          12
4 2020-02-03          2 PC           2
5 2020-02-08          3 UV         200

注意:日期列是日期格式。

运用你的算法

根据您的尝试,您希望提取前 7 天。
由于我们有一个“日期”列,我们可以在这里使用一个日期函数来帮助我们。 {lubridate}day() 提取“天数”。

> df %>% filter(day(date) <= 7) 
# A tibble: 4 x 4
  date       identifier category views
  <date>          <dbl> <chr>    <dbl>
1 2020-02-01          1 TV          27
2 2020-02-02          2 PC          40
3 2020-02-03          1 TV          12
4 2020-02-03          2 PC           2

前 7 天以外的所有内容均已消失。

接下来您想summarise 获取您的产品浏览总数。

df %>% 
## ---------- c.f. above ------------
  filter(day(date) <= 7) %>% 
## ---------- summarise in bins that you need := groups -------
  group_by(identifier, category) %>% 
  summarise(total_views = sum(views)
            , .groups = "drop" )      # if grouping is not needed "drop" it

这给了你:

# A tibble: 2 x 3
  identifier category total_views
       <dbl> <chr>          <dbl>
1          1 TV                39
2          2 PC                42

现在选择top-10 并排序:

df %>% 
## ---------- c.f. above ------------
  filter(day(date) <= 7) %>% 
  group_by(identifier, category) %>% 
  summarise(total_views = sum(views), .groups = "drop" ) %>% 
## ---------- make use of another helper function of dplyr
  top_n(n = 10, total_views) %>%    # note top-10 makes here no "real" sense :), try top_n(1, total_views) 
  arrange(desc(total_views))      # arrange in descending order on total_views

换行

现在工作流程已经到位,请考虑将您的代码分解成您认为有用的块。

我把这个留给你。您可以将中间结果分配给新的数据帧,并将数据的准备工作包装到一个函数中,然后将 top_n() %&gt;% arrange() 包装到另一个函数中,...

这会产生:

# A tibble: 2 x 3
  identifier category total_views
       <dbl> <chr>          <dbl>
1          2 PC                42
2          1 TV                39

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    相关资源
    最近更新 更多