将一年添加到只有几个月和几天的时间序列

Adding a year to a time series with only months and days

我有一个这样的df

    month   day   x
1   1        1    84
2   1        2    43
3   1        3    49
4   1        4    67
5   1        5    59
......
366 12       31   97

从 10 月到 12 月应该是 2019 年,从 1 月到 9 月应该是 2020 年

我尝试使用

df$year<-as.date(df,origin='2019-01-01')

但是我不知道怎么去争论

我想让年份列得到一个日期列然后试试

df$date<-as.date(with(paste("???",month,day,sep="-"), %Y-%m-%d,origin ="2019-01-01")

但我又不知道如何为年份争论

任何帮助都会为我节省很多时间,因为手动完成似乎不可能

您可以使用如下所示的内容。如果您需要一个固定变量而不是 2019/2020,您可以在 oct-dec 时使用 var-1,在 jan - sep.

时使用 var
library(dplyr)
library(lubridate)


df1 %>% 
  mutate(date = if_else(month %in% c(10:12),
                        ymd(paste(2019, df1$month, df1$day, sep = "-")),
                        ymd(paste(2020, df1$month, df1$day, sep = "-"))))

数据:

df1 <- data.frame(month = c(1:12), day = 1, x = 5)

我们可以将 ifelse 语句与来自 lubridate 的 make_date 函数一起使用:

library(dplyr)
library(lubridate)
df %>% 
  mutate(year= ifelse(month %in% c(10,11,12), 2019, 2020),
         date = make_date(year, month, day))

输出:

    month day  x year       date
1       1   1 84 2020 2020-01-01
2       1   2 43 2020 2020-01-02
3      11   3 49 2019 2019-11-03
4       1   4 67 2020 2020-01-04
5       1   5 59 2020 2020-01-05
366    12  31 97 2019 2019-12-31

使用基本特征,您可以使用 rowSums 识别 10 月的第 31,然后 ISOdate

w <- which.max(rowSums(d[1:2]) == 31 + 10)

d$year <- c(rep(2020, w), rep(2019, 365 - w))

d$date <- do.call(\(year, month, day, ...) as.Date(ISOdate(year, month, day)), d)

结果

head(d, 3)
#   month day  x year       date
# 1     1   1 58 2020 2020-01-01
# 2     1   2 74 2020 2020-01-02
# 3     1   3 43 2020 2020-01-03

tail(d, 3)
#     month day  x year       date
# 363    12  29 46 2019 2019-12-29
# 364    12  30 82 2019 2019-12-30
# 365    12  31 63 2019 2019-12-31

注:

R.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L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), day = c(1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 
18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 
31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 
27L, 28L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 
26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 
28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 
25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 
30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 
27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L), 
    x = c(72L, 95L, 95L, 76L, 84L, 64L, 85L, 84L, 70L, 95L, 75L, 
    64L, 72L, 48L, 68L, 68L, 44L, 53L, 46L, 49L, 62L, 53L, 74L, 
    86L, 58L, 63L, 85L, 85L, 81L, 44L, 66L, 82L, 86L, 90L, 75L, 
    54L, 53L, 52L, 47L, 48L, 61L, 95L, 96L, 73L, 59L, 57L, 94L, 
    70L, 81L, 68L, 83L, 83L, 95L, 55L, 73L, 51L, 50L, 83L, 58L, 
    45L, 74L, 64L, 54L, 60L, 77L, 94L, 90L, 47L, 44L, 50L, 70L, 
    69L, 76L, 69L, 62L, 63L, 62L, 55L, 47L, 43L, 71L, 47L, 66L, 
    69L, 74L, 53L, 85L, 62L, 53L, 57L, 52L, 65L, 85L, 68L, 62L, 
    43L, 72L, 69L, 79L, 71L, 95L, 45L, 96L, 70L, 96L, 51L, 48L, 
    67L, 52L, 48L, 72L, 54L, 64L, 79L, 49L, 55L, 90L, 57L, 51L, 
    63L, 79L, 69L, 48L, 52L, 89L, 70L, 95L, 64L, 75L, 95L, 70L, 
    94L, 95L, 43L, 87L, 56L, 46L, 53L, 60L, 91L, 61L, 88L, 83L, 
    89L, 45L, 87L, 69L, 83L, 71L, 44L, 93L, 96L, 80L, 46L, 80L, 
    66L, 80L, 59L, 86L, 51L, 48L, 80L, 81L, 79L, 65L, 80L, 72L, 
    84L, 61L, 55L, 49L, 54L, 60L, 44L, 44L, 84L, 49L, 94L, 45L, 
    80L, 79L, 51L, 70L, 48L, 66L, 89L, 60L, 57L, 76L, 86L, 88L, 
    71L, 79L, 94L, 74L, 93L, 80L, 75L, 90L, 91L, 77L, 95L, 48L, 
    90L, 77L, 50L, 49L, 56L, 71L, 73L, 62L, 85L, 90L, 76L, 67L, 
    44L, 96L, 52L, 73L, 85L, 44L, 44L, 79L, 89L, 93L, 58L, 57L, 
    75L, 48L, 58L, 59L, 51L, 64L, 89L, 82L, 76L, 51L, 56L, 46L, 
    82L, 48L, 76L, 93L, 60L, 52L, 75L, 77L, 53L, 52L, 56L, 50L, 
    66L, 70L, 67L, 87L, 90L, 50L, 80L, 54L, 81L, 54L, 73L, 88L, 
    64L, 52L, 64L, 73L, 79L, 68L, 53L, 86L, 94L, 56L, 62L, 65L, 
    85L, 61L, 54L, 93L, 60L, 69L, 82L, 83L, 56L, 51L, 82L, 71L, 
    76L, 77L, 60L, 79L, 61L, 83L, 87L, 43L, 74L, 76L, 63L, 59L, 
    54L, 93L, 82L, 65L, 89L, 68L, 62L, 61L, 91L, 89L, 79L, 59L, 
    52L, 80L, 71L, 96L, 46L, 84L, 47L, 92L, 80L, 86L, 64L, 88L, 
    56L, 93L, 94L, 66L, 46L, 87L, 63L, 89L, 92L, 88L, 65L, 90L, 
    71L, 53L, 91L, 61L, 91L, 62L, 62L, 48L, 80L, 73L, 62L, 75L, 
    59L, 72L, 61L, 90L, 51L, 66L, 74L, 58L, 73L, 89L, 50L, 79L, 
    90L, 94L, 59L, 47L, 88L, 83L)), row.names = c(NA, -365L), class = "data.frame")

基础 R 选项 -

transform(df, date = as.Date(paste(ifelse(month %in% 10:12, 2019, 2020), month, day, sep = '-')))

#    month day  x       date
#1       1   1 84 2020-01-01
#2       1   2 43 2020-01-02
#3      11   3 49 2019-11-03
#4       1   4 67 2020-01-04
#5       1   5 59 2020-01-05