I have a df like this
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
The year should be 2019 from Oct-Dec and 2020 from Jan-Sep
I tried to use
df$year<-as.date(df,origin='2019-01-01')
But I am not sure how to make an argument.
I want the year column to get a date column and try then
df$date<-as.date(with(paste("???",month,day,sep="-"), %Y-%m-%d,origin ="2019-01-01")
but again I don't know how to make an argument for year
Please any help would save me a lot of time because doing it manually seems impossible
CodePudding user response:
We could use an ifelse
statement with the make_date
function from lubridate:
library(dplyr)
library(lubridate)
df %>%
mutate(year= ifelse(month %in% c(10,11,12), 2019, 2020),
date = make_date(year, month, day))
output:
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
CodePudding user response:
You could use something like below. If you need a fixed variable instead of 2019/2020 you can use something like var-1
when it is oct-dec and var
when it is jan - sep.
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 = "-"))))
data:
df1 <- data.frame(month = c(1:12), day = 1, x = 5)
CodePudding user response:
Using base features you could use rowSums
to identify 31th of October, then 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)
Result
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
Note:
R.version.string
# [1] "R version 4.1.1 (2021-08-10)"
Data:
d <- structure(list(month = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1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L, 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")
CodePudding user response:
Base R option -
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