I import dates from Excel into R.
Some of the values appear as real numbers:
e.g. 43313.458055555559
I would like to convert these numbers into a
timestamp representation like this - 9/4/2018 11:45:35AM
Thanks in advance.
Based on G. Grothendieck's comment, try:
as.POSIXct(as.Date(43313.458055555559, origin = "1899-12-30"))
Just replace 43313.458055555559 by DataFrame.name$Column.name.
Spreadsheets like Microsoft Excel on a Windows PC or OpenOffice.org
represent datetimes as days and fraction of days since December 30,
1899 (usually). If x is a vector of such numbers then
as.Date("1899-12-30") + floor(x) will give a vector of Date class
dates with respect to Date’s origin. Similarly chron("12/30/1899") + x
will give chron dates relative to chron’s origin. Excel on a Mac
usually represents dates as days and fraction of days since January 1,
1904 so as.Date("1904-01-01") + floor(x) and chron("01/01/1904") + x convert vectors of numbers representing such dates to Date and chron respectively. Its possible
to set Excel to use either origin which is why the word usually was
employed above.
Source: https://www.r-project.org/doc/Rnews/Rnews_2004-1.pdf
Related
I have a vector of date values:
dates=c("43018","43343","42272","06/27/17","01/10/18","10/11/18")
This is a mixture of actual dates and the Excel number-value of dates (ie: number of days since January 1, 1900). I want to convert all of these values to the Excel format of dates, so we would have an output that looks like the following:
dates
[1] "43018" "43343" "42272" "42913" "43110" "43384"
My goal is to take these values and subtract them from another vector with an equal number of date values that are all the same to get an age of each observation.
Can anyone help point me in the right direction? Thank you!
Figured it out - use the "janitor" library and the excel_numeric_to_date function.
Badda bing badda boom.
I'm trying to convert a column of full integers into date format of abbreviated months. The column has numbers like : 01 02 04 15 13. etc. I want these numbers to show the month they correspond to. Could someone please tell me how. the code I'm trying is this:
#Changing integers to Month Abbrev.
dets_per_month$monthcollected = as.POSIXlt(dets_per_month$monthcollected, format = "%m", origin = "%m")
but I realize the column doesn't have an origin because it's not in date format.
month.abb[as.integer(dets_per_month$monthcollected)]
I would recommend the lubridate package for all things date-time related. It's a nifty package and has more utility than base R, but YMMV.
library(lubridate)
x <- rep(1:12, 2)
lubridate::month(x, label=TRUE)
How can set R to count months instead of dates when converting integers to dates?
After reading several threads on how to convert dates in R, it seems like nobody has asked how it is possible to convert numeric dates if the numerics is given in monthly timeseries. E.g. 552 represents January 2006.
I have tried several things, such as using as.Date(dates,origin="1899-12-01"), but I reckognize that R counts days instead of months. Thus, the code on year-month number 552 above yields "1901-06-06" instead of the correct 2006-01-01.
Sidenote: I also want the format to be YEARmonth, but does R allow displaying dates without days?
I think your starting date should be '1960-01-01'.
anyway you can solve this problem using the package lubridate.
in this case you can start from a date and add months.
library(lubridate)
as.Date('1960-01-01') %m+% months(552)
it gives you
[1] "2006-01-01"
you can display only the year and month of a date, but in that case R coerces the date into a character.
format(as.Date('2006-01-01'), "%Y-%m")
I have a .xlsx file that I read onto R. This file has one of the columns in date format (d/m/y) but for some reason it's displaying as series of numbers in the data frame on RStudio.
My question is how do I change the column to the original date format?
Here's an example of the date that's showing: 887587200 - instead of something like 12/03/1974.
Any help to fix this would be appreciated.
Thanks
Looks like your dates are being stored as a numeric value, likely the number of seconds since Jan 1, 1970. So to convert the column, you could do:
df$my_col <- as.Date(df$my_col / 60 / 60 / 24, origin = '1970-01-01')
This converts 887587200 to a date of 1998-02-16.
I have some numbers that represent dates in milliseconds since epoch, 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970
1365368400000,
1365973200000,
1366578000000
I'm converting them to date format:
as.Date(as.POSIXct(my_dates/1000, origin="1970-01-01", tz="GMT"))
answer:
[1] "2013-04-07" "2013-04-14" "2013-04-21"
How to convert these strings back to milliseconds since epoch?
Here are your javascript dates
x <- c(1365368400000, 1365973200000, 1366578000000)
You can convert them to R dates more easily by dividing by the number of milliseconds in one day.
y <- as.Date(x / 86400000, origin = "1970-01-01")
To convert back, just convert to numeric and multiply by this number.
z <- as.numeric(y) * 86400000
Finally, check that the answer is what you started with.
stopifnot(identical(x, z))
As per the comment, you may sometimes get numerical rounding errors leading to x and z not being identical. For numerical comparisons like this, use:
library(testthat)
expect_equal(x, z)
I will provide a simple framework to handle various kinds of dates encoding and how to go back an forth. Using the R package ‘lubridate’ this is made very easy using the period and interval classes.
When dealing with days, it can be easy as one can use the as.numeric(Date) to get the number of dates since the epoch. To get any unit of time smaller than a day one can convert using the various factors (24 for hours, 24 * 60 for minutes, etc.) However, for months, the math can get a bit more tricky and thus I prefer in many instances to use this method.
library(lubridate)
as.period(interval(start = epoch, end = Date), unit = 'month')#month
This can be used for year, month, day, hour, minute, and smaller units through apply the factors.
Going the other way such as being given months since epoch:
library(lubridate)
epoch %m+% as.period(Date, unit = 'months')
I presented this approach with months as it might be the more complicated one. An advantage to using period and intervals is that it can be adjusted to any epoch and unit very easily.