I have a data frame with a column of date objects. How can i get a new column with the number of a month?
example: January -> 1, february -> 2 …
I need a new column with the numbers of each day of the month, too.
example: 2022-01-01 -> 1 , 2022-01-02 – 2
>Solution :
You can use the following code:
df = data.frame(date = as.Date(c("2022-01-01", "2022-01-02")))
df[, "month"] <- format(df[,"date"], "%m")
df
Output:
date month
1 2022-01-01 01
2 2022-01-02 01