Converting Date formats in R [closed] - r

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
dates <- as.Date(dli$Dates)
class(dates)
[1] "Date"
dates
[1] "2016-01-01" "2016-01-02" "2016-01-03" "2016-01-04" "2016-01-05" "2016-01-06"
[7] "2016-01-07" "2016-01-08" "2016-01-09" "2016-01-10" "2016-01-11" "2016-01-12"
[13] "2016-01-13" "2016-01-14" "2016-01-15" "2016-01-16" "2016-01-17" "2016-01-18"
[19] "2016-01-19" "2016-01-20" "2016-01-21" "2016-01-22" "2016-01-23" "2016-01-24"
[25] "2016-01-25" "2016-01-26" "2016-01-27" "2016-01-28" "2016-01-29" "2016-01-30"
[31] "2016-01-31" "2016-02-01" "2016-02-02" "2016-02-03" "2016-02-04" "2016-02-05"
[37] "2016-02-06" "2016-02-07" "2016-02-08" "2016-02-09" "2016-02-10" "2016-02-11"
This is my date format , so i need to convert it into "2016-month-day"
I am getting NA values
dates <- as.Date(dli$Dates,"%d/%b/%Y")
class(dates)
[1] "Date"
dates
[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[31] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[61] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[91] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[121] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
can you give any suggestions
Thanks in Advance

Good practice is to store date in R as YYYY-MM-DD, and your strings already seem to be at the good format, but :
The format you're passing to as.Date must describe what the strings contain, not what you're expecting as an output.
"%d/%b/%Y" stands for "day as a number (0-31) slash abbreviated month slash 4-digit year", and your strings format are "4-digit year - month as a number - day as a number".
If you want to format the date, you need to call format :
> date <- "2016-01-01"
> date <- as.Date(date, format = "%Y-%m-%d")
> date
[1] "2016-01-01"
> format(date, "%d/%b/%Y")
[1] "01/jan/2016"

To obtain your required format i.e., 2016-month-day , you can use format function once you have converted vector of strings to Date type.
I hope below code snippet clears your doubt.
> d = c("2016-02-08","2016-02-18","2015-02-08","2016-02-02")
> class(d)
[1] "character"
> d = as.Date(d)
> class(d)
[1] "Date"
> d = format(d,"%Y-%b-%d")
> d
[1] "2016-Feb-08" "2016-Feb-18" "2015-Feb-08" "2016-Feb-02"
Format function converts the date type objects into the required format. Refer to this link for more information on date type formatting.

If you just want to render your dates in this format, then use format:
x <- as.Date("2016-01-01")
format(x, "%Y %b %a %d")
[1] "2016 Jan Fri 01"
There is a separation of concerns here. If you already have your date information stored in R as date types, then you need not change anything internally to extract further information from those dates.
Demo

You would use as.Date() to convert between dates saved as character and Date objects.
If you want to change the format of a Date object, you can use format().
You have specified "2016-month-day" as the desired format of the dates in the question, but in the code you provide you are using "%d/%b/%Y". The way this works is: the % indicates that the next character will be a conversion specification, everything else (e.g. (- or /) will be used for finding / adding delimiter to the date representation. (see ?strptime for details).
So in your case, just use
dates <- format(dli$Dates, format = "%Y-%b-%d")
to get the result specified in the text of the question:
[1] "2016-Jan-01" "2016-Jan-02" "2016-Jan-03" "2016-Jan-04" "2016-Jan-05"
or this:
dates <- format(dli$Dates, format = "%Y/%b/%d")
to get what you have used in the code snipped:
[1] "2016/Jan/01" "2016/Jan/02" "2016/Jan/03" "2016/Jan/04" "2016/Jan/05"

You can use the package lubridate to convert to a date with ymd then format it for the way you want it displayed
Dates_df <- mutate(Dates, dli = format(ymd(dli), "%Y/%b/%d")
(I use dplyr here, I assume you have other variables in Dates)
without dplyr if you just want to keep the dates in a vector:
Dates_vec <- format(ymd(Dates$dli), "%Y/%b/%d")

Related

Convert column of time (as character decimals from Excel) to time in R

I'm trying to convert a column of time (which I imported from Excel) that R has converted into a decimal/character string back into hh:mm:ss time. I have seen many good answers (using library chron, for example), but I keep getting these errors:
My data:
> head(env$Time, 10)
[1] "0.41736111111111113" "0.6020833333333333" "0.45" "0.47222222222222227" "0.5131944444444444"
[6] "0.51250000000000007" "0.47361111111111115" "0.44791666666666669" "0.35138888888888892" "0.45277777777777778"
times(env$Time)
Error in convert.times(times., fmt) : format h:m:s may be incorrect
In addition: Warning message:
In unpaste(times, sep = fmt$sep, fnames = fmt$periods, nfields = 3) :
8173 entries set to NA due to wrong number of fields
chron(times(env$Time))
Error in convert.times(times., fmt) : format h:m:s may be incorrect
In addition: Warning message:
In unpaste(times, sep = fmt$sep, fnames = fmt$periods, nfields = 3) :
8173 entries set to NA due to wrong number of fields
strptime(env$Time, format = "%H:%M:%S")
[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[38] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Found this answer here: How to express a decimal time in HH:MM
x <- as.numeric(env$Time) # Store time variable as numeric vector
env$Time2 <- sub(":00$", "", round(times(x), "min")) # Run this code to save as new column in dataframe (note, don't need to divide by 24 if decimal is fraction of a day like my data is

format input to ts object in R

I am having some data with a time column expressed in week.year and a corresponding unit that was measured in that week.
Week-Year Units
01.2020 39.12727273
02.2020 33.34545455
03.2020 118.7181818
04.2020 83.71818182
05.2020 58.56985
. .
52.2020 89.54651534
I have to create a ts object which takes these Week-Year values as input.
The reason for requiring this step is- there are sometimes values missing for certain weeks so using an auto generated time scale (start=, end=, frequency=) will mess up the readings.
Is there any way of achieving it? or is there any way to accommodate such a situation?
R novice here, would really appreciate some guidance. :)
Assuming the input is the data frame DF shown reproducibly in the Note at the end, convert it to a zoo object and then use as.ts to create a ts series with frequency 52.
library(zoo)
week <- as.integer(DF[[1]])
year <- as.numeric(sub("...", "", DF[[1]]))
z <- zoo(DF[[2]], year + (week - 1) / 52)
tt <- as.ts(z)
tt
## Time Series:
## Start = c(2020, 1)
## End = c(2020, 52)
## Frequency = 52
## [1] 39.12727 33.34545 118.71818 83.71818 58.56985 NA NA
## [8] NA NA NA NA NA NA NA
## [15] NA NA NA NA NA NA NA
## [22] NA NA NA NA NA NA NA
## [29] NA NA NA NA NA NA NA
## [36] NA NA NA NA NA NA NA
## [43] NA NA NA NA NA NA NA
## [50] NA NA 89.54652
frequency(tt)
## [1] 52
class(tt)
## [1] "ts"
Note
Lines <- " Week-Year Units
01.2020 39.12727273
02.2020 33.34545455
03.2020 118.7181818
04.2020 83.71818182
05.2020 58.56985
52.2020 89.54651534"
DF <- read.table(text = Lines, header = TRUE, colClasses = c("character", NA))

Using as.Date returns mostly NAs, but some correct dates

I am trying to convert date information from a .csv file to date format in R, so that I create two week intervals with which to categorize data. However, when using as.Date, it is only working correctly on some of the dates, and leaving the rest as NA. I have tried editing the format of the date cells in excel, I have tried rewriting every single date cell in excel from scratch. I have tried changing my locale. I have tried everything I can find as a possible solution online. Please help!
> lions <- read.csv("Lions_30m.csv")
> dates<-as.character(lions$Date)
> typeof(dates)
[1] "character"
> dates
[1] "5/1/2017" "5/9/2017" "5/21/2017" "4/17/2017" "4/21/2017" "5/12/2017"
"3/27/2017" "3/13/2017"
[9] "4/10/2017" "4/26/2017" "4/23/2017" "3/7/2017" "5/28/2017" "4/27/2017"
"5/16/2017" "4/5/2017"
[17] "2/16/2017" "2/18/2017" "4/26/2017" "2/26/2017" "2/26/2017" "5/18/2017"
"3/4/2017" "4/14/2017"
[25] "3/3/2017" "3/31/2017" "3/11/2017" "3/19/2017" "3/22/2017" "3/23/2017"
"3/25/2017" "4/13/2017"
[33] "4/14/2017" "4/15/2017" "4/17/2017" "4/19/2017" "4/23/2017" "4/29/2017"
"5/15/2017" "3/26/2017"
[41] "5/6/2017"
> date <-as.Date(dates, format='%d/%m/%Y')
> date
[1] "2017-01-05" "2017-09-05" NA NA NA "2017-12-05" NA
[8] NA "2017-10-04" NA NA "2017-07-03" NA NA
[15] NA "2017-05-04" NA NA NA NA NA
[22] NA "2017-04-03" NA "2017-03-03" NA "2017-11-03" NA
[29] NA NA NA NA NA NA NA
[36] NA NA NA NA NA "2017-06-05"
You want %m/%d/%Y, not %d/%m/%Y, unless you live in a weird country where each year has 21 months....
We can also do this automatically with anytime that picks up the format based on the input
library(anytime)
anydate(c("4/26/2017", "2/26/2017", "2/26/2017" ))
#[1] "2017-04-26" "2017-02-26" "2017-02-26"
like this
dates <- c("5/1/2017", "5/9/2017", "5/21/2017", "4/17/2017", "4/21/2017", "5/12/2017",
"3/27/2017", "3/13/2017", "4/10/2017", "4/26/2017", "4/23/2017", "3/7/2017",
"5/28/2017", "4/27/2017", "5/16/2017", "4/5/2017", "2/16/2017", "2/18/2017",
"4/26/2017", "2/26/2017", "2/26/2017", "5/18/2017", "3/4/2017", "4/14/2017",
"3/3/2017", "3/31/2017", "3/11/2017", "3/19/2017", "3/22/2017", "3/23/2017",
"3/25/2017", "4/13/2017", "4/14/2017", "4/15/2017", "4/17/2017", "4/19/2017",
"4/23/2017", "4/29/2017", "5/15/2017", "3/26/2017", "5/6/2017")
use class() to see what is it, so t speak
class(dates)
#> [1] "character"
as_date_dates <- as.Date(dates, "%m/%d/%Y")
class(as_date_dates)
#> [1] "Date"
or the lubridate way to avoid the %m/%d/%Y-%d/%m/%Y confusion. Kinda same idea akrun is going with,
# install.packages(c("lubridate"), dependencies = TRUE)
library(lubridate)
as_lubridate_dates <- mdy(dates)
class(as_lubridate_dates)
#> [1] "Date"

Formatting dates with only month and year in R

I have quite a simple problem that I've not found anywhere on here.
I have date format:
times = c("Dec_2011" , "July_2011", "Dec_2010" ,"July_2010" , "Dec_2009" , "July_2009", "Dec_2008" ,
"July_2008" ,"Dec_2007" , "July_2007", "Dec_2006" , "July_2006" ,"Dec_2005" , "July_2005",
"Dec_2004" , "July_2004" ,"Dec_2003" , "July_2003", "Dec_2002" , "July_2002", "Dec_2001" ,
"July_2001", "Dec_2000" , "July_2000")
How can I get these into date format:
31-07-2000, 31-07-2001, etc...
31-12-2000, 31-12-2001, etc...
I've tried:
times <- format(as.Date(time, "%B_%Y")
times
[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
times <- format(as.Date(time, "%B_%Y), "31-%m-%Y)
times
[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
times <- as.Date(paste("31", times, sep="-"), "%d-%m-%Y")
times
[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
times <- format(as.Date(time, "%b_%Y"), "31-%m-%Y")
# NA
I'm not quite sure how to proceed.
If we need 31 as the day for all the elements, use paste to join 31, convert to Date class and get the desired format with format.
format(as.Date(paste(times, "31", sep="_"), "%b_%Y_%d"), "%d-%m-%Y")
#[1] "31-12-2011" "31-07-2011" "31-12-2010" "31-07-2010" "31-12-2009" "31-07-2009" "31-12-2008" "31-07-2008" "31-12-2007" "31-07-2007" "31-12-2006"
#[12] "31-07-2006" "31-12-2005" "31-07-2005" "31-12-2004" "31-07-2004" "31-12-2003" "31-07-2003" "31-12-2002" "31-07-2002" "31-12-2001" "31-07-2001"
#[23] "31-12-2000" "31-07-2000"
Instead of manually pasteing 31, we can automate this with as.yearmon from zoo. The advantage is that for months that have less than 31 days, we get the last day by doing that.
library(zoo)
format(as.Date(as.yearmon(times, "%b_%Y"), frac=1), "%d-%m-%Y")
#[1] "31-12-2011" "31-07-2011" "31-12-2010" "31-07-2010" "31-12-2009" "31-07-2009" "31-12-2008" "31-07-2008" "31-12-2007" "31-07-2007" "31-12-2006"
#[12] "31-07-2006" "31-12-2005" "31-07-2005" "31-12-2004" "31-07-2004" "31-12-2003" "31-07-2003" "31-12-2002" "31-07-2002" "31-12-2001" "31-07-2001"
#[23] "31-12-2000" "31-07-2000"

Using a date field in a ts?

I wonder how I can make use of an already existing date field when creating a ts in R.
Sometimes you simply have a date before you have a ts object, e.g.
x <- as.Date("2008-01-01") + c(30,60,90,120,150)
# add some data to it
df = data.frame(datefield=x,test=1:length(x))
Now, is there a way to use the datefield of the df to as an index when creating a ts object? Because:
ts(df$test,start=c(2008,1,2),frequency=12)
(obviuously) completely ignores the date information I already have. Making use of ts methods like acf is the reason why I´d like to make it a ts object. I typcically use monthly an quarterly time series...
You don't necessarily need to create new types of objects from scratch; you can always coerce to other classes, including ts as you need to. zoo or xts are arguably to most useful and intuitive but there are others. Here is your example, cast as a zoo object, which we then coerce to class ts for use in acf().
## create the data
x <- as.Date("2008-01-01") + c(30,60,90,120,150)
df = data.frame(datefield=x,test=1:length(x))
## load zoo
require(zoo)
## convert to a zoo object, with order given by the `datefield`
df.zoo <- with(df, zoo(test, order.by = x))
## or to a regular zoo object
df.zoo2 <- with(df, zooreg(test, order.by = x))
Now we can easily go to a ts object using the as.ts() method:
> as.ts(df.zoo)
Time Series:
Start = 13920
End = 14040
Frequency = 0.0333333333333333
[1] 1 2 3 4 5
> ## zooreg object:
> as.ts(df.zoo2)
Time Series:
Start = 13909
End = 14029
Frequency = 1
[1] 1 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[21] NA NA NA NA NA NA NA NA NA NA 2 NA NA NA NA NA NA NA NA NA
[41] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[61] 3 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[81] NA NA NA NA NA NA NA NA NA NA 4 NA NA NA NA NA NA NA NA NA
[101] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[121] 5
Notice the two ways in which the objects are represented (although we could have made the zooreg version the same as the standard zoo object by setting the frequency argument to 0.03333333):
> as.ts(with(df, zooreg(test, order.by = datefield,
+ frequency = 0.033333333333333)))
Time Series:
Start = 13920.0000000001
End = 14040.0000000001
Frequency = 0.033333333333333
[1] 1 2 3 4 5
We can use the zoo/zooreg object in acf() and it will get the correct lags (daily observations but every 30 days):
acf(df.zoo)
Whether this is intuitive to you or not depends on how you view the time series. We can do the same thing in terms of a 30-day interval via:
acf(coredata(df.zoo))
where we use coredata() to extract the time series itself, ignoring the date information.
I don't know exactly what you're trying to do, but acf also works on simple vectors, given off course it represents a regular time series (i.e. even spaced). Otherwise the result is just bollocks.
>acf(df$test)
Regarding the ts object :
The "dates" you see are just from the print.ts function, so they're not inherent to the ts object. The ts object has no date information in it. You can set the option calender=FALSE to get the standard print out of the ts object.
> ts(df$test,start=2008,frequency=12)
Jan Feb Mar Apr May
2008 1 2 3 4 5
> print(ts(df$test,start=2008,frequency=12),calendar=F)
Time Series:
Start = c(2008, 1)
End = c(2008, 5)
Frequency = 12
[1] 1 2 3 4 5
Now, the vector you construct looks like :
> x
[1] "2008-01-31" "2008-03-01" "2008-03-31" "2008-04-30" "2008-05-30"
which is or isn't regular, depending on how you see it. If you extract the months, then you have 1 observation for january, 2 for march, 1 for april...: not regular. You have an observation every 30 days : regular. If you have an observation every 30 days, you shouldn't bother about the dates as 365 is not dividable through 30. Hence, one year you'll have 12 observations, another one you'll have 13 observations. So you can't set the frequency in ts in a consequent correct way.
So I'd refrain from using a ts all together, as James already indicated in the comments.
If you want:
Use the date information you already have
Easily set the frequency to a desired value
End up with a ts object
You can start with an xts object, add a frequency attribute, and then convert to ts:
library("xts")
my_xts <- xts(df$test, df$datefield)
attr(my_xts, 'frequency') <- 12 # Set the frequency
my_ts <- as.ts(my_xts)
The resulting ts object will have the specified period and will have the correct dates assigned to each data point.

Resources