Questions tagged [as.date]

as.Date is an R function used to convert between character or numeric representations and objects of class "Date", representing calendar dates.

as.Date is an R function used to convert between character or numeric representations and objects of class "Date", representing calendar dates.

https://www.rdocumentation.org/packages/base/versions/3.3.2/topics/as.Date

349 questions
113
votes
8 answers

What are the "standard unambiguous date" formats for string-to-date conversion in R?

Please consider the following $ R --vanilla > as.Date("01 Jan 2000") Error in charToDate(x) : character string is not in a standard unambiguous format But that date clearly is in a standard unambiguous format. Why the error message? Worse, an…
Matt Dowle
  • 58,872
  • 22
  • 166
  • 224
55
votes
4 answers

Convert date-time string to class Date

I have a data frame with a character column of date-times. When I use as.Date, most of my strings are parsed correctly, except for a few instances. The example below will hopefully show you what is going on. # my attempt to parse the string to Date…
Btibert3
  • 38,798
  • 44
  • 129
  • 168
30
votes
5 answers

Convert week number to date

I have a data frame in R with the week of the year that I would like to convert to a date. I know I have to pick a year and a day of the week so I am fixing those values at 2014 and 1. Converting this to a date seems simple:…
Aaron Soderstrom
  • 599
  • 1
  • 6
  • 12
25
votes
3 answers

How to avoid date formatted values getting converted to numeric when assigned to a matrix or data frame?

I have run into an issue I do not understand, and I have not been able to find an answer to this issue on this website (I keep running into answers about how to convert dates to numeric or vice versa, but that is exactly what I do not want to…
Joes de Natris
  • 373
  • 1
  • 3
  • 5
19
votes
2 answers

Fastest way to parse a date-time string to class Date

I have a column with dates as character in the format 10/17/2017 12:00:00 AM. I want parse the string and keep only the date part as class Date, i.e. 2017-10-17. I am using - df$ReportDate = as.Date(df$ReportDate, format = "%m/%d/%Y %I:%M:%S %p")…
phil_t
  • 851
  • 2
  • 7
  • 17
11
votes
4 answers

R set default origin for as.Date

Is there a way to set default origin for as.Date? I did this function to workaround: as.date=function(x, origin='1970-01-01') as.Date(x, origin=origin) For example: as.Date(0) Error in as.Date.numeric(0) : 'origin' deve ser…
xm1
  • 1,663
  • 1
  • 17
  • 28
10
votes
3 answers

as.Date produces unexpected result in a sequence of week-based dates

I am working on the transformation of week based dates to month based dates. When checking my work, I found the following problem in my data which is the result of a simple call to as.Date() as.Date("2016-50-4", format =…
KoenV
  • 4,113
  • 2
  • 23
  • 38
9
votes
1 answer

R: Setting limits to scale_x_yearqtr in ggplot for yearqtr (zoo)

I'm working with the data set resembling the extract below: head(nomis.lng.agg) quarter decile avg.val 1 2004 Q4 1 5.680000 2 2005 Q1 1 5.745763 3 2005 Q2 1 5.503341 4 2005 Q3 1…
Konrad
  • 17,740
  • 16
  • 106
  • 167
8
votes
2 answers

R script working locally not working on shinyapp.io

I'm trying to put together a R + Shiny app that, at least initially, plots a histogram of date data. I have it working just fine on my local system in RStudio, but in shinyapps.io it doesn't work. The app, at the moment, is very simple - the…
Nik T
  • 83
  • 1
  • 4
7
votes
4 answers

as.Date with two-digit years

If I convert the date 10.10.61 (DD.MM.YY) with as.Date(date, format="%d.%m.%y") for some reason it converts it into 2061-10-10. Is there an elegant way to correct for this or do I have to do it manually by slicing the string and adding "19" in…
D. Studer
  • 1,711
  • 1
  • 16
  • 35
7
votes
2 answers

Setting xlim with dates in R

I need to set the x-axis limit of a plot in R, but my values are dates (%m/%d/%Y format). How would I go about changing this? I am trying to plot trophic position vs. collection date. All of my collection dates are in date format (%m/%d/%Y) This is…
M. Bittner
  • 71
  • 1
  • 1
  • 2
7
votes
4 answers

as.Date from 'YYYY.mm' format

I have a data frame where date is stored as a double e.g., 1993.09 1993.10 1993.11 1993.12 I want to convert this into a date format '%Y %m %d' (with days always 1). As far as I understand, as.Date() wants a string input. However, for some reason…
Zlo
  • 1,150
  • 2
  • 18
  • 38
7
votes
2 answers

R Read abbreviated month form a date that is not in English

I have a text file that includes dates, and want to convert it to a datatable. Converting the dates such as 03-FEB-2011 can be done with data$fecha <- as.Date(data$textDate , "%d-%b-%Y") The problem is that the column is in Spanish, so I don't get…
Maria Velasco
  • 191
  • 2
  • 3
  • 8
6
votes
2 answers

How to extract correct date from POSIXct element?

How can I get the correct date from the first column in my code? test <- data.frame(posixdate = c("2013-05-01 00:59:00", "2013-05-01 01:59:00", "2013-05-01 02:59:00", "2013-05-01 03:59:00")) test$posixdate <- as.POSIXct(test$posixdate,…
Aki
  • 409
  • 2
  • 6
  • 15
5
votes
2 answers

R Convert to date from multiple formats

I need to convert a string of dates that is in multiple formats to valid dates. e.g. dates <- c("01-01-2017","02-01-2017","12-01-2016","20160901","20161001", "20161101") > as.Date(dates, format=c("%m-%d-%Y","%Y%m%d")) [1] "2017-01-01" NA …
user3357059
  • 1,122
  • 1
  • 15
  • 30
1
2 3
23 24