如何按分钟在R中按组填写缺少的日期

时间:2019-05-17 13:51:19

标签: r date fill minute

我正在尝试从具有不同组的数据框中填写缺少的分钟。我希望遗漏的分钟用零填充。

我尝试使用此R - Fill missing dates by group,但找不到填补遗漏分钟的方法。

Datetime            | Group | Value |
2019-01-01 00:00:00 |  1    |  5    |
2019-01-01 00:00:00 |  2    |  4    |
2019-01-01 00:00:00 |  3    |  2    | 
2019-01-01 00:01:00 |  1    |  1    |
2019-01-01 00:02:00 |  1    |  2    | 
2019-01-01 00:02:00 |  2    |  2    |
2019-01-01 00:02:00 |  3    |  1    |
2019-01-01 00:03:00 |  1    |  1    |
2019-01-01 00:03:00 |  2    |  2    |
2019-01-01 00:04:00 |  1    |  1    |

我希望决赛桌看起来像这样-

Datetime            | Group | Value |
2019-01-01 00:00:00 |  1    |  5    |
2019-01-01 00:00:00 |  2    |  4    |
2019-01-01 00:00:00 |  3    |  2    | 
2019-01-01 00:01:00 |  1    |  1    |
2019-01-01 00:01:00 |  2    |  0    | 
2019-01-01 00:01:00 |  3    |  0    |
2019-01-01 00:02:00 |  1    |  2    |
2019-01-01 00:02:00 |  2    |  2    |
2019-01-01 00:02:00 |  3    |  1    |
2019-01-01 00:03:00 |  1    |  1    |
2019-01-01 00:03:00 |  2    |  2    |
2019-01-01 00:03:00 |  3    |  0    |
2019-01-01 00:04:00 |  1    |  1    |
2019-01-01 00:04:00 |  2    |  0    |
2019-01-01 00:04:00 |  3    |  0    |

3 个答案:

答案 0 :(得分:0)

struct User {
    let name : String
    var age : Int
    var height : Double
    var weight : Double
    var activityLevel = [1,2,3,4,5,6,7,8,9,10]
}

数据

library(dplyr); library(padr)
df %>%
  pad(group = 'Group', interval = 'min') %>%   # Explicitly fill by 1 min
  fill_by_value(Value)

#pad applied on the interval: min
#              Datetime Group Value
#1  2019-01-01 00:00:00     1     5
#2  2019-01-01 00:01:00     1     1
#3  2019-01-01 00:02:00     1     2
#4  2019-01-01 00:03:00     1     1
#5  2019-01-01 00:04:00     1     1
#6  2019-01-01 00:00:00     2     4
#7  2019-01-01 00:01:00     2     0    # added
#8  2019-01-01 00:02:00     2     2
#9  2019-01-01 00:03:00     2     2
#10 2019-01-01 00:00:00     3     2
#11 2019-01-01 00:01:00     3     0    # added
#12 2019-01-01 00:02:00     3     1

答案 1 :(得分:0)

使用data/c/SNAPSHOT.jar data/g/SNAPSHOT.jar

base

答案 2 :(得分:0)

我们可以使用complete

library(tidyverse)
df %>%
   complete(Group, Datetime = seq(min(Datetime),
          max(Datetime), by = "1 min"), fill = list(Value = 0)) %>% 
   arrange(Datetime)  %>% 
   select(names(df))
# A tibble: 15 x 3
#   Datetime            Group Value
#   <dttm>              <dbl> <dbl>
# 1 2019-01-01 00:00:00     1     5
# 2 2019-01-01 00:00:00     2     4
# 3 2019-01-01 00:00:00     3     2
# 4 2019-01-01 00:01:00     1     1
# 5 2019-01-01 00:01:00     2     0
# 6 2019-01-01 00:01:00     3     0
# 7 2019-01-01 00:02:00     1     2
# 8 2019-01-01 00:02:00     2     2
# 9 2019-01-01 00:02:00     3     1
#10 2019-01-01 00:03:00     1     1
#11 2019-01-01 00:03:00     2     2
#12 2019-01-01 00:03:00     3     0
#13 2019-01-01 00:04:00     1     1
#14 2019-01-01 00:04:00     2     0
#15 2019-01-01 00:04:00     3     0

数据

df <- structure(list(Datetime = structure(c(1546300800, 1546300800, 
1546300800, 1546300860, 1546300920, 1546300920, 1546300920, 1546300980, 
1546300980, 1546301040), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    Group = c(1, 2, 3, 1, 1, 2, 3, 1, 2, 1), Value = c(5, 4, 
    2, 1, 2, 2, 1, 1, 2, 1)), row.names = c(NA, -10L), class = "data.frame")