Converting Date to Name - r

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))

Related

how to select/subset certain dates in my data set in r

I have a data set for 10 years. I want to select or subset the data by fiscal year using date variable. For date variable is a character. For instance, I want to select data for fiscal year 2020 which is from 01-10-2019 to 30-09-2020. How can I do this in R ?
Here is an example using zoo package:
df1 <- structure(list(dateA = structure(c(14974, 18628, 14882, 16800,
17835, 16832, 16556, 15949, 16801), class = "Date")), row.names = c(NA,
-9L), class = c("tbl_df", "tbl", "data.frame"))
library(dplyr)
library(zoo)
df1 %>%
mutate(fiscal_year = as.integer(as.yearmon(dateA) - 4/12 +1))
output:
dateA fiscal_year
<date> <int>
1 2010-12-31 2011
2 2021-01-01 2021
3 2010-09-30 2011
4 2015-12-31 2016
5 2018-10-31 2019
6 2016-02-01 2016
7 2015-05-01 2016
8 2013-09-01 2014
9 2016-01-01 2016
as said by #r2evans you should post a minimal reprex.
However with the few information you posted maybe this worked example may fit:
date_vect <- c('01-10-2019','30-07-2020','15-07-2019','03-03-2020')
date_vect[substr(date_vect,7,12) == "2020"]
Under the hypothesis that you have a vector of dates in a string format. You may want to pick all the strings with the last four character equal to 2020 (the year you're interested in).
P.S: It's good practice to use the appropriate format when dealing with dates. You can unlock other features such as ordering with R date libraries.

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))

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

Harmonizing dates

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")

convert quarter year to last date of quarter in R

I have an issue when I use as.Date(as.yearqtr(test[,1],format ="%qQ%Y"),frac =1), but it returns an error,and quater-year didn't change to date. The error is:
error in as.yearqtr(as.numeric(x)) (list) object cannot be coerced to type 'double'
This is my dataframe in R.
TIME VALUE
1Q2019 1
2Q2019 2
3Q2019 3
4Q2019 4
The ideal output is
TIME VALUE
2019-03-31 1
2019-06-30 2
2019-09-30 3
2019-12-31 4
We can convert to Date with zoo and get the last date of the quarter with frac. We use some RegEx to rearrange in zoo's suitable format:
df$TIME=as.Date(as.yearqtr(gsub("(\\d)(Q)(\\d{1,})","\\3 Q\\1",df$TIME)),frac = 1)
df
TIME VALUE
1 2019-03-31 1
2 2019-06-30 2
3 2019-09-30 3
4 2019-12-31 4
Data:
df <-structure(list(TIME = structure(1:4, .Label = c("1Q2019", "2Q2019",
"3Q2019", "4Q2019"), class = "factor"), VALUE = 1:4), class = "data.frame", row.names = c(NA,
-4L))
Here is a function that will return a vector of dates, given an input vector in the form of 1Q2019...
dateStrings <- c("1Q2019","2Q2019","3Q2019","4Q2019","1Q2020")
lastDayOfQuarter <- function(x){
require(lubridate)
result <- NULL
months <-c(3,6,9,12)
days <- c(31,30,30,31)
for(i in 1:length(x)) {
qtr <- as.numeric(substr(x[i],1,1))
result[i] <- mdy(paste(months[qtr],days[qtr],(substr(x[i],3,6)),sep="-"))
}
as.Date(result)
}
lastDayOfQuarter(dateStrings)
and the output:
>lastDayOfQuarter(dateStrings)
[1] "2019-03-31" "2019-06-30" "2019-09-30" "2019-12-31" "2020-03-31"
>

Resources