How to convert Year-Month 'Character Value' into 'Date Value' in R - r

I am trying to change the Character value into Date value to make the data reactive to dateRangeinput in R shiny. However, I believe that the Dates should be in Date format or value, not in Character or String value.
I am having a problem to convert them. Below is the code
CompletedProject$EndDate <- ymd(substring(CompletedProject$EndDate, first = 1, last = 10))
CompletedProject$EndDate <- format(as.Date(CompletedProject$EndDate), "%Y-%m")
CompletedProject_monthly <- subset(CompletedProject_monthly, CompletedProject_monthly$EndDate >= input$IMdaterange[1] & CompletedProject_monthly$EndDate <= input$IMdaterange[2])
> class(CompletedProject$EndDate)
[1] "character"
I have tried as.Date function, but it didn't work.
I can do it with year month day format easily, but year month format is harder.
I need the date format as year-month.
Appreciate for help experts!!

Related

How to fix character dates and add zeros to it in R?

I have a dataset that all of it’s date variables are messed up. All of the columns are characters. They look like this:
name <- c(“Ana”, “Maria”, “Rachel”, “Julia”)
date_of_birth <- c(“9/8/1997”, “22/3/1966”, “24/10/1969”, “25/6/2019”)
data <- as.data.frame(cbind(name, date_of_bieth))
I need to turn those dates into dd/mm/yyyy format. They are already in this order, but I need to add zero when dd or mm has only one digit.
For example, “9/8/1997” should be “09/08/1997”.
We can try this
> format(as.Date(date_of_birth, format = "%d/%m/%Y"), "%d/%m/%Y")
[1] "09/08/1997" "22/03/1966" "24/10/1969" "25/06/2019"

convert year and month into date format

I have dates in format
192607 192608
and want to transform them so that they are in the following format and can be used for a xts object
1926-07-01 1926-08-01
I have tried working with as.date and paste() but couldn't make it work.
Help is very much appreciated. Thank you!!
You need to paste then put format date. Something like this:
dates <- c("192607", "192608")
dates <- paste0(dates,"01")
dates <- as.Date(dates, format ="%Y%m%d")
dates
The result is
[1] "1926-07-01" "1926-08-01"
Assuming all the dates will be converted to the first of the month, this lubridate solution works.
library(lubridate)
dates <- c(192607, 192608)
dates <- paste0(dates, '01') # add 01 for day of month
# output: "19260701" "19260801"
dates <- ymd(dates)
# output: "1926-07-01" "1926-08-01"

Format POSIX scenario in Dates

Create a variable of value 15Aug1947 and 15Aug2018 in POSIX Date format.
Find the number of days elapsed since Independence as of 15th August 2018.
Need to code in R language.
DATE1 <- c("15Aug1947")
DATE2 <- c("15Aug2018")
X <- as.Date(DATE1, "%d/%m/%y") - as.Date(DATE2 , "%d/%m/%y")
print(X)
You are close, but are missing a small detail. The second argument in as.Date requires you to specify exactly in what format your dates is coming from. Right now, you are saying your date is comprised of 15/08/1947. Two things are wrong with this. Your date has no slashes and the month is not an integer but an abbreviation of the month name. The correct way to parse this date would be
> ps <- "%d%b%Y"
> DATE1 <- c("15Aug1947")
> DATE2 <- c("15Aug2018")
> X <- as.Date(DATE1, ps) - as.Date(DATE2 , ps)
>
> print(X)
Time difference of -25933 days
For more information on how to construct the string for parsing, see ?strptime.
You can use a package to parse dates automatically, such as lubridate.
The following code may help!
#Create a variable of value 15Aug1947 and 15Aug2018 in POSIX Date format
dt <- c(as.POSIXct("15Aug1947", format = "%d%b%Y"),as.POSIXct("15Aug1948", format = "%d%b%Y"))
#Finding the number of days elapsed
difftime(dt[2], dt[1], units = "days")
#Time difference of 25933 days

r intersect of date in with year and month

I would like to find the intersection of two dataframes based on the date column.
Previously, I have been using this command to find the intersect of a yearly date column (where the date only contained the year)
common_rows <-as.Date(intersect(df1$Date, df2$Date), origin = "1970-01-01")
But now my date column for df1 is of type date and looks like this:
1985-01-01
1985-04-01
1985-07-01
1985-10-01
My date column for df2 is also of type date and looks like this (notice the days are different)
1985-01-05
1985-04-03
1985-07-07
1985-10-01
The above command works fine when I keep the format like this (i.e year, month and day) but since my days are different and I am interested in the monthly intersection I dropped the days like this, but that produces and error when I look for the intersection:
df1$Date <- format(as.Date(df1$Date), "%Y-%m")
common_rows <-as.Date(intersect(df1$Date, df2$Date), origin = "1970-01-01")
Error in charToDate(x) :
character string is not in a standard unambiguous format
Is there a way to find the intersection of the two datasets, based on the year and month, while ignoring the day?
The problem is the as.Date() function wrapping your final output. I don't know if you can convert incomplete dates to date objects. If you are fine with simple strings then use common_rows <-intersect(df1$Date, df2$Date). Otherwise, try:
common_rows <-as.Date(paste(intersect(df1$Date, df2$Date),'-01',sep = ''), origin = "1970-01-01")
Try this:
date1 <- c('1985-01-01','1985-04-01','1985-07-01','1985-10-01')
date2 <- c('1985-01-05','1985-04-03','1985-07-07','1985-10-01')
# extract the part without date
date1 <- sapply(date1, function(j) substr(j, 1, 7))
date2 <- sapply(date2, function(j) substr(j, 1, 7))
print(intersect(date1, date2))
[1] "1985-01" "1985-04" "1985-07" "1985-10"

How can I reformat a series of dates in a vector in R

I have vector of dates that i'm trying to convert with as date but i'm not getting the expected output, when I sapply with as.Date instead of getting a series of reformatted dates I get the names as dates and some odd value.
dates = c("20-Mar-2015", "25-Jun-2015", "23-Sep-2015", "22-Dec-2015")
sapply(dates, as.Date, format = "%d-%b-%Y")
20-Mar-2015 25-Jun-2015 23-Sep-2015 22-Dec-2015
16514 16611 16701 16791
I would like each of the values in the vector to be showing the new formated value. E.g. like what would happen if as.Date was shown applied to each element
as.Date("20-Mar-2015", format = "%d-%b-%Y")
[1] "2015-03-20"
You can directly use as.Date(dates, format = "%d-%b-%Y"). as.Date is vectorized, i.e. it can take a vector as input, not only a single entry.
In your case:
dates <- c("20-Mar-2015", "25-Jun-2015", "23-Sep-2015", "22-Dec-2015")
as.Date(dates, format = "%d-%b-%Y")
# [1] "2015-03-20" "2015-06-25" "2015-09-23" "2015-12-22"

Resources