Harmonizing dates - r

I have a data frame with dates and the time in it.
Now I want to convert each date into the correct month. How can I do this?
Now it looks like this:
1 01.01.2019 00:00:20.747000
2 21.04.2019 00:00:21.362000
3 31.08.2019 00:00:21.422000
I need it in a format like this:
1 01.01.2019
2 21.04.2019
3 31.08.2019
or eventually like this:
1 January
2 April
3 August

With base R, you can do the following.
First, I wasn't sure if initial data frame was in POSIXct format. I converted it for my example.
Then you can use format to extract the month number or month name.
lubridate is a great package to use for various date manipulations as well and has month function.
df$datetime <- as.POSIXct(df$datetime, format = "%d.%m.%Y %H:%M:%OS")
df$date_only <- as.Date(df$datetime)
df$month_num <- format(df$datetime, "%m")
df$month <- format(df$datetime, "%B")
df
Output
datetime date_only month_num month
1 2019-01-01 00:00:20 2019-01-01 01 January
2 2019-04-21 00:00:21 2019-04-21 04 April
3 2019-08-31 00:00:21 2019-08-31 08 August
Data
df <- structure(list(datetime = c("01.01.2019 00:00:20.747000", "21.04.2019 00:00:21.362000",
"31.08.2019 00:00:21.422000")), class = "data.frame", row.names = c(NA,
-3L))

Try:
df$date <- lubridate::dmy_hms(df$date)
df$date <- format(df$date, "%d.%m.%Y")
data:
df: structure(list(date = c("01.01.2019", "21.04.2019", "31.08.2019"
)), row.names = c(NA, -3L), class = "data.frame")

Related

converting to hh:mm:ss format in r

How would I convert seconds into h/m/s format. I've tried to use seconds_to_period but it only gives the value in seconds. e.g
ID Time
1 345 secs
2 121 secs
3 78 secs
I want this is in HH:MM:SS format how is this done?
We may use hms from hms after converting to period
library(lubridate)
df1$Time <- hms::hms(seconds_to_period(readr::parse_number(df1$Time)))
-output
> df1
ID Time
1 1 00:05:45
2 2 00:02:01
3 3 00:01:18
data
df1 <- structure(list(ID = 1:3, Time = c("345 secs", "121 secs", "78 secs"
)), class = "data.frame", row.names = c(NA, -3L))

extract year and month from character date field R

I have a column in my large data set called Date. How do I extract both the year and month from it? I would like to create a column Month where the month goes from 1-12 and year where the year goes from the first year in my data set to the last year in my data set.
Thanks.
> typeof(data$Date)
[1] "character
> head(data$Date)
[1] "2/06/2020 11:23" "12/06/2020 7:56" "12/06/2020 7:56" "29/06/2020 16:54" "3/06/2020 15:09" "25/06/2020 17:11"
dplyr and lubridate -
library(dplyr)
library(lubridate)
data <- data %>%
mutate(Date = dmy_hm(Date),
month = month(Date),
year = year(Date))
# Date month year
#1 2020-06-02 11:23:00 6 2020
#2 2020-06-12 07:56:00 6 2020
#3 2020-06-12 07:56:00 6 2020
#4 2020-06-29 16:54:00 6 2020
#5 2020-06-03 15:09:00 6 2020
#6 2020-06-25 17:11:00 6 2020
Base R -
data$Date <- as.POSIXct(data$Date, tz = 'UTC', format = '%d/%m/%Y %H:%M')
data <- transform(data, Month = format(Date, '%m'), Year = format(Date, '%Y'))
data
data <- structure(list(Date = c("2/06/2020 11:23", "12/06/2020 7:56",
"12/06/2020 7:56", "29/06/2020 16:54", "3/06/2020 15:09", "25/06/2020 17:11"
)), class = "data.frame", row.names = c(NA, -6L))

Converting Date to Name

I have date's in a dataframe with corresponding sampling date as presented by the sample dataframe:
Date Temp
2016-06-11 5
2017-08-19 12
2018-01-21 13
2019-04-28 7
The date column is in numeric format currently. I want to convert the numeric month (i.e. 06) into its full name (i.e. June) but am having trouble with the conversion.
I did check the converting dates to names question but was confused by the select DATENAME.
You may simply use months(). Example:
d <- transform(d, date.m=months(v))
d
# date x date.m
# 1 2020-10-01 -1.1390886 October
# 2 2020-11-01 -0.6872151 November
# 3 2020-12-01 1.0632769 December
# 4 2021-01-01 1.7351265 January
Note: If your date is not of class "date" you also need to wrap as.Date:
d <- transform(d, date.m=months(as.Date(v)))
Data:
d <- structure(list(date = structure(c(18536, 18567, 18597, 18628), class = "Date"),
x = c(-1.13908860117162, -0.687215137639502, 1.06327693201579,
1.73512650928455)), class = "data.frame", row.names = c(NA,
-4L))

convert datetime to three letter month and year in R [duplicate]

This question already has answers here:
Extract month and year from a zoo::yearmon object
(7 answers)
Extract Month and Year From Date in R
(5 answers)
Closed 2 years ago.
I need help to convert the first column of datetime in 3 letter month and year.
DF<-
Datetime ID Name
2020-01-01 10:12:14 I-1 Rnad
2020-01-01 16:32:43 I-2 Rnxa
Required output
Datetime ID Name Month
2020-01-01 10:12:14 I-1 Rnad Jan-20
2020-01-01 16:32:43 I-2 Rnxa Jan-20
You can use the format function with strptime abbreviations.
my_df$Month <- format(my_df$Datetime, format = "%b-%y")
Try this Sophia:
#Code
df$Month <- format(as.POSIXct(df$Datetime,format='%Y-%m-%d %H:%M:%S',
tz = 'GMT'),"%b-%y")
Output:
df
Datetime ID Name Month
1 2020-01-01 10:12:14 I-1 Rnad Jan-20
2 2020-01-01 16:32:43 I-2 Rnxa Jan-20
Some data used:
#Data
df <- structure(list(Datetime = c("2020-01-01 10:12:14", "2020-01-01 16:32:43"
), ID = c("I-1", "I-2"), Name = c("Rnad", "Rnxa")), row.names = c(NA,
-2L), class = "data.frame")

Prophet Date Format R

year_month amount_usd
201501 -390217.24
201502 230944.09
201503 367259.69
201504 15000.00
201505 27000.21
201506 38249.65
df <- structure(list(year_month = 201501:201506, amount_usd = c(-390217.24,
230944.09, 367259.69, 15000, 27000.21, 38249.65)), class = "data.frame", row.names = c(NA,
-6L))
I want to bring it in to DD/MM/YYYY format for usability in Prophet Forecasting code.
this is what i have tried so far.
for (loopitem in loopvec){
df2 <- subset(df, account_id==loopitem)
df3 <- df2[,c("year_month","amount_usd")]
df3$year_month <- as.Date(df3$year_month, format="YYYY-MM", origin="1/1/1970")
try <- prophet(df3, seasonality.mode = 'multiplicative')
}
Error in fit.prophet(m, df, ...) :
Dataframe must have columns 'ds' and 'y' with the dates and values respectively.
You need to paste the day number (I'm just using the first) to the year_month values, then can use the ymd() function from lubridate to convert the column to a date object.
library(dplyr)
library(lubridate)
mutate_at(df, "year_month", ~ymd(paste(., "01")))
year_month amount_usd
1 2015-01-01 -390217.24
2 2015-02-01 230944.09
3 2015-03-01 367259.69
4 2015-04-01 15000.00
5 2015-05-01 27000.21
6 2015-06-01 38249.65

Resources