How to convert a character string date to date class if day value is missing
Try as.yearmon
from the zoo
package:
library(zoo)
# as.yearmon(TS, "%Y-%m")
as.yearmon(TS)
# [1] "Dec 2004" "Jan 2005" "Feb 2005" "Mar 2005" "Apr 2005" "May 2005" "Jun 2005"
# [8] "Jul 2005" "Aug 2005" "Sep 2005" "Oct 2005" "Nov 2005" "Dec 2005" "Jan 2006"
# [15] "Feb 2006" "Mar 2006" "Apr 2006" "May 2006" "Jun 2006" "Jul 2006" "Aug 2006"
as.Date(as.yearmon(TS))
## [1] "2004-12-01" "2005-01-01" "2005-02-01" "2005-03-01" "2005-04-01"
## [6] "2005-05-01" "2005-06-01" "2005-07-01" "2005-08-01" "2005-09-01"
## [11] "2005-10-01" "2005-11-01" "2005-12-01" "2006-01-01" "2006-02-01"
## [16] "2006-03-01" "2006-04-01" "2006-05-01" "2006-06-01" "2006-07-01"
## [21] "2006-08-01"
format(as.Date(as.yearmon(TS)), "%Y-%m")
## [1] "2004-12" "2005-01" "2005-02" "2005-03" "2005-04" "2005-05" "2005-06"
## [8] "2005-07" "2005-08" "2005-09" "2005-10" "2005-11" "2005-12" "2006-01"
## [15] "2006-02" "2006-03" "2006-04" "2006-05" "2006-06" "2006-07" "2006-08"
What are the standard unambiguous date formats for string-to-date conversion in R?
This is documented behavior. From ?as.Date
:
format: A character string. If not specified, it will try
'"%Y-%m-%d"' then '"%Y/%m/%d"' on the first non-'NA' element,
and give an error if neither works.
as.Date("01 Jan 2000")
yields an error because the format isn't one of the two listed above. as.Date("01/01/2000")
yields an incorrect answer because the date isn't in one of the two formats listed above.
I take "standard unambiguous" to mean "ISO-8601" (even though as.Date
isn't that strict, as "%m/%d/%Y" isn't ISO-8601).
If you receive this error, the solution is to specify the format your date (or datetimes) are in, using the formats described in the Details section in ?strptime
.
Make sure that the order of the conversion specification as well as any separators correspond exactly with the format of your input string. Also, be sure to use particular care if your data contain day/month names and/or abbreviations, as the conversion will depend on your locale (see the examples in ?strptime
and read ?LC_TIME
; see also strptime
, as.POSIXct
and as.Date
return unexpected NA
).
Convert string to date, format: dd.mm.yyyy
The format is case-sensitive ("%y" is ambiguous and system dependent, I believe):
as.Date(D, "%d.%m.%Y")
[1] "1948-12-06"
The help topic ?strptime
has details:
‘%y’ Year without century (00-99). On input, values 00 to 68 are
prefixed by 20 and 69 to 99 by 19 - that is the behaviour
specified by the 2004 and 2008 POSIX standards, but they do
also say ‘it is expected that in a future version the default
century inferred from a 2-digit year will change’.
R datetime series missing values
This is the approach I took, using ifelse()
under the assumption that you are dealing with two possibilities - with and without seconds
date_time <- c("2020-01-18 20:12:16", "2020-01-18 20:12")
ifelse(nchar(date_time) == 16,
format(as.POSIXct(date_time, format="%Y-%m-%d %H:%M"), "%Y-%m-%d %H:%M:%S"),
format(as.POSIXct(date_time, format="%Y-%m-%d %H:%M:%S"), "%Y-%m-%d %H:%M:%S"))
Convert String to Date with Milliseconds
The Date class stores the time as milliseconds, and if you look into your date object you will see that it actually has a time of 1598515567413 milliseconds.
You are fooled by the System.out.println() which uses Date's toString() method. This method is using the "EEE MMM dd HH:mm:ss zzz yyyy" format to display the date and simply omits all milliseconds.
If you use your formatter, which has milliseconds in its format string, you will see that the milliseconds are correct:
System.out.println(formatter.format(dateFormatter));
outputs 2020-08-27T10:06:07.413
R - character string with week-Year: week is lost when converting to Date format
You just need to add a weekday to your weeks.strings
in order to make the dates unambiguous (adapted from Jim Holtman's answer on R-help).
as.Date(paste(weeks.strings,1),"%Y-%U %u")
As pointed out in the comments, the Date class is not appropriate if the dates span a long horizon because--at some point--the chosen weekday will not exist in the first/last week of the year. In that case you could use a numeric vector where the whole portion is the year and the decimal portion is the fraction of weeks/year. For example:
wkstr <- sprintf("%d-%02d", rep(2000:2012,each=53), 0:52)
yrwk <- lapply(strsplit(wkstr, "-"), as.numeric)
yrwk <- sapply(yrwk, function(x) x[1]+x[2]/53)
Related Topics
Changing Styles When Selecting and Deselecting Multiple Polygons with Leaflet/Shiny
Remove Weekend Data in a Dataframe
Variable Assignment Within a For-Loop
Can You Pass a Vector to a Vararg: Vector to Sprintf
Create a Dataframe with Random Numbers in Each Column
How to Apply Geom_Smooth() for Every Group
Count the Number of Integer Digits
Ggplot2': Label Values of Barplot That Uses 'Fun.Y="Mean"' of 'Stat_Summary'
How to Create a Variable of Rownames
Getting the Column Names of a Data Frame with Sapply
Using R to Connect to a Sharepoint List
Shiny: Unwanted Space Added by Plotoutput() And/Or Renderplot()
Setting Individual Y Axis Limits with Facet Wrap Not with Scales Free_Y