【问题标题】:unlist a column with a list of dates in dplyr在 dplyr 中取消列出包含日期列表的列
【发布时间】:2018-10-07 03:49:12
【问题描述】:

我一直在考虑使用 tidyr 中的 unnest() 函数来处理包含日期列表的列。

x <- seq(from= as.POSIXct('2011-01-01 14:00:00'),length.out=100,by = "hour")

y <- seq(from= as.POSIXct('2012-01-01 14:00:00'),length.out=100,by = "hour")
df <- data.frame(x,y)

当我尝试为每一行创建一个列表,然后取消嵌套它。我收到以下错误。

df %>% rowwise() %>% mutate(sequence = list(seq.POSIXt(x,y,"10 min"))) %>% unnest(sequence)

错误:每一列必须是向量列表或数据框列表[序列]

其他人可以帮忙吗?我已经用数字做到了这一点,并且 unnest 函数工作正常。但是,它似乎不适用于包含日期/日期时间的列表。

【问题讨论】:

    标签: r dplyr tidyr


    【解决方案1】:

    seq.POSIXt() 的结果强制转换为数据框并列出该列表...

    x <- seq(from= as.POSIXct('2011-01-01 14:00:00'),length.out=100,by = "hour")
    y <- seq(from= as.POSIXct('2012-01-01 14:00:00'),length.out=100,by = "hour")
    df <- data.frame(x,y)
    
    library(dplyr)
    library(tidyr)
    
    df %>% 
      rowwise() %>% 
      mutate(sequence = list(data.frame(seq.POSIXt(x, y, "10 min")))) %>% 
      unnest(sequence)
    
    # # A tibble: 5,256,100 x 3
    #    x                   y                   seq.POSIXt.x..y...10.min..
    #    <dttm>              <dttm>              <dttm>                    
    #  1 2011-01-01 14:00:00 2012-01-01 14:00:00 2011-01-01 14:00:00       
    #  2 2011-01-01 14:00:00 2012-01-01 14:00:00 2011-01-01 14:10:00       
    #  3 2011-01-01 14:00:00 2012-01-01 14:00:00 2011-01-01 14:20:00       
    #  4 2011-01-01 14:00:00 2012-01-01 14:00:00 2011-01-01 14:30:00       
    #  5 2011-01-01 14:00:00 2012-01-01 14:00:00 2011-01-01 14:40:00       
    #  6 2011-01-01 14:00:00 2012-01-01 14:00:00 2011-01-01 14:50:00       
    #  7 2011-01-01 14:00:00 2012-01-01 14:00:00 2011-01-01 15:00:00       
    #  8 2011-01-01 14:00:00 2012-01-01 14:00:00 2011-01-01 15:10:00       
    #  9 2011-01-01 14:00:00 2012-01-01 14:00:00 2011-01-01 15:20:00       
    # 10 2011-01-01 14:00:00 2012-01-01 14:00:00 2011-01-01 15:30:00       
    # # ... with 5,256,090 more rows
    

    【讨论】:

      【解决方案2】:

      如果我没记错的话data.frame 不支持很好的列表列。 尝试用df &lt;- tibble::tibble(x, y)替换df &lt;- data.frame(x,y)


      library(dplyr)
      library(tidyr)
      x <- seq(from= as.POSIXct('2011-01-01 14:00:00'),length.out=100,by = "hour")
      
      y <- seq(from= as.POSIXct('2012-01-01 14:00:00'),length.out=100,by = "hour")
      df <- tibble::tibble(x,y)
      
      
      df %>% rowwise() %>% mutate(sequence = list(seq.POSIXt(x,y,"10 min"))) %>% unnest(sequence)
      #> # A tibble: 5,256,100 x 3
      #>    x                   y                   sequence           
      #>    <dttm>              <dttm>              <dttm>             
      #>  1 2011-01-01 14:00:00 2012-01-01 14:00:00 2011-01-01 14:00:00
      #>  2 2011-01-01 14:00:00 2012-01-01 14:00:00 2011-01-01 14:10:00
      #>  3 2011-01-01 14:00:00 2012-01-01 14:00:00 2011-01-01 14:20:00
      #>  4 2011-01-01 14:00:00 2012-01-01 14:00:00 2011-01-01 14:30:00
      #>  5 2011-01-01 14:00:00 2012-01-01 14:00:00 2011-01-01 14:40:00
      #>  6 2011-01-01 14:00:00 2012-01-01 14:00:00 2011-01-01 14:50:00
      #>  7 2011-01-01 14:00:00 2012-01-01 14:00:00 2011-01-01 15:00:00
      #>  8 2011-01-01 14:00:00 2012-01-01 14:00:00 2011-01-01 15:10:00
      #>  9 2011-01-01 14:00:00 2012-01-01 14:00:00 2011-01-01 15:20:00
      #> 10 2011-01-01 14:00:00 2012-01-01 14:00:00 2011-01-01 15:30:00
      #> # ... with 5,256,090 more rows
      

      【讨论】:

      • 我尝试将数据帧转换为 tibble 格式。而且我似乎仍然遇到同样的错误。
      • 奇怪,我也运行了这段代码,得到了与@theArun 首次发布的相同的错误
      • 我使用的是 tidyr 0.8.0 版。
      【解决方案3】:

      我无法重现该错误,但我认为另一种方法可能会有所帮助。

      library(dplyr)
      library(tidyr)
      
      df %>% 
        rowwise() %>% 
        mutate(sequence = paste(seq.POSIXt(x, y, "10 min"), collapse=",")) %>%
        ungroup() %>%
        separate_rows(sequence, sep=",") %>%
        mutate(sequence = as.POSIXct(sequence))
      

      如果你想使用unnest 那么

      df %>% 
        rowwise() %>% 
        mutate(sequence = list(seq.POSIXt(x, y, "10 min"))) %>% 
        ungroup() %>%
        unnest(sequence)
      

      输出为:

         x                   y                   sequence           
         <dttm>              <dttm>              <dttm>             
       1 2011-01-01 14:00:00 2011-01-02 14:00:00 2011-01-01 14:00:00
       2 2011-01-01 14:00:00 2011-01-02 14:00:00 2011-01-01 14:10:00
       3 2011-01-01 14:00:00 2011-01-02 14:00:00 2011-01-01 14:20:00
       4 2011-01-01 14:00:00 2011-01-02 14:00:00 2011-01-01 14:30:00
       5 2011-01-01 14:00:00 2011-01-02 14:00:00 2011-01-01 14:40:00
      ...
      

      样本数据:

      df <- structure(list(x = structure(c(1293870600L, 1293874200L, 1293877800L, 
      1293881400L, 1293885000L, 1293888600L, 1293892200L, 1293895800L, 
      1293899400L, 1293903000L), class = c("POSIXct", "POSIXt"), tzone = ""), 
          y = structure(c(1293957000L, 1293960600L, 1293964200L, 1293967800L, 
          1293971400L, 1293975000L, 1293978600L, 1293982200L, 1293985800L, 
          1293989400L), class = c("POSIXct", "POSIXt"), tzone = "")), .Names = c("x", 
      "y"), row.names = c(NA, -10L), class = "data.frame")
      

      【讨论】:

      • 谢谢总理。请问你用的是什么版本的R?
      • 我当前的机器有R version 3.4.0
      • 这很奇怪,我有相同的版本,但我仍然得到错误。我们能够在办公室的另一台机器上重现它。
      • 我会在家里测试一下,看看能否重现错误。顺便说一句,您可以尝试在您的机器上重新安装 dplyrtidyr
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 2021-07-03
      • 2016-06-02
      • 1970-01-01
      相关资源
      最近更新 更多