【问题标题】:Is there a better way to fetch multiple variables using the NESTING function to COMPLETE the dataframe?有没有更好的方法来使用 NESTING 函数获取多个变量来完成数据帧?
【发布时间】:2019-06-22 10:03:29
【问题描述】:

我正在尝试 complete 为几个分类变量创建一个数据框,因此,使用 nesting 函数为数据中存在的分类变量的每个组合创建一个连贯的时间序列对象.

这是一个示例数据框 -

> dput(df)
structure(list(ds = structure(c(1546300800, 1546387200, 1546473600, 
1546560000), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    y = c(40, 40, 40, 40), type = c("a", "a", "a", "b"), city = c("x", 
    "x", "x", "y"), hid = c(1, 2, 2, 3)), row.names = c(NA, -4L
), na.action = structure(c(`5` = 5L), class = "omit"), class = c("tbl_df", 
"tbl", "data.frame"))

# Find the date range
min_date <- min(df$ds)
max_date <- max(df$ds)
dates_seq <- seq.POSIXt(from = min_date, 
                        to = max_date, 
                        by = '1 day')

这是我尝试过的,它给出了预期的结果 -

df %>%
    complete(nesting(type, city, hid), 
             ds = dates_seq, 
             fill = list(y = 0))

# A tibble: 12 x 5
#   type  city    hid ds                      y
#   <chr> <chr> <dbl> <dttm>              <dbl>
# 1 a     x         1 2019-01-01 00:00:00    40
# 2 a     x         1 2019-01-02 00:00:00     0
# 3 a     x         1 2019-01-03 00:00:00     0
# 4 a     x         1 2019-01-04 00:00:00     0
# 5 a     x         2 2019-01-01 00:00:00     0
# 6 a     x         2 2019-01-02 00:00:00    40
# 7 a     x         2 2019-01-03 00:00:00    40
# 8 a     x         2 2019-01-04 00:00:00     0
# 9 b     y         3 2019-01-01 00:00:00     0
#10 b     y         3 2019-01-02 00:00:00     0
#11 b     y         3 2019-01-03 00:00:00     0
#12 b     y         3 2019-01-04 00:00:00    40

如果我不明确知道df 中哪些是分类变量,我如何将这些列传递给nesting?我的假设是 df 的所有实例至少包含两个 ds, y 列。


编辑:我还尝试了以下方法,这会引发 错误 -

complete(df, 
    nesting(names(df)[!(names(df) %in% c("ds", "y"))]), 
    ds = dates_seq, 
    fill = list(y = 0))

【问题讨论】:

    标签: r dplyr time-series tidyr


    【解决方案1】:

    我们可以使用rlang 包。对names(df)[!names(df) %in% c("ds", "y")]使用syms(因为有多个列)并存储在一个变量中,然后在nesting函数中使用!!!

    library(tidyverse)
    library(rlang)
    
    ne <- syms(names(df)[!names(df) %in% c("ds", "y")])
    
    df %>%
      complete(nesting(!!!ne), 
               ds = dates_seq, 
               fill = list(y = 0))
    # # A tibble: 12 x 5
    #    type  city    hid ds                      y
    #    <chr> <chr> <dbl> <dttm>              <dbl>
    #  1 a     x         1 2019-01-01 00:00:00    40
    #  2 a     x         1 2019-01-02 00:00:00     0
    #  3 a     x         1 2019-01-03 00:00:00     0
    #  4 a     x         1 2019-01-04 00:00:00     0
    #  5 a     x         2 2019-01-01 00:00:00     0
    #  6 a     x         2 2019-01-02 00:00:00    40
    #  7 a     x         2 2019-01-03 00:00:00    40
    #  8 a     x         2 2019-01-04 00:00:00     0
    #  9 b     y         3 2019-01-01 00:00:00     0
    # 10 b     y         3 2019-01-02 00:00:00     0
    # 11 b     y         3 2019-01-03 00:00:00     0
    # 12 b     y         3 2019-01-04 00:00:00    40
    

    【讨论】:

      【解决方案2】:

      这是使用!!! 运算符解决此问题的另一种方法 -

      df %>%
          complete(nesting(!!!select(df, -ds, -y)), 
                   ds = dates_seq, 
                   fill = list(y = 0))
      

      【讨论】:

        猜你喜欢
        • 2010-11-11
        • 1970-01-01
        • 2021-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多