Convert series of numbers to date in R - r

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.

Related

How do I subset and manipulate time series data with lubridate and dplyr in Rstudio?

I have loaded in time series data of creek depth and am needing to calculate total annual values (Oct-April) for each year. The following is what I have tried thus far:
depth <- read_csv("Depth.Water_Depth-_All_Instruments#GCNP_-_Robber's_Roost_Spring.EntireRecord .csv")
The following is a screenshot of the resulting data frame
enter image description here
These are my attempts at making the timestamp column (ISO_UTC) into a date class. Although, each attempt makes all values for ISO_UTC into N/A values instead of dates / times.
ymd_hm(depth$ISO_UTC)
depth$ISO_UTC<- as.Date(depth$ISO_UTC, format = "%Y-%m-%dT%H:%M")
depth$ISO_UTC<- as.POSIXct(depth$ISO_UTC, format = "%Y-%m-%d %H:%M", tz = "US/Pacific")
Please help me to put these data into usable datetime values.
Please see the above details.

My data does not convert to time series in R

My data contains several measurements in one day. It is stored in CSV-file and looks like this:
enter image description here
The V1 column is factor type, so I'm adding a extra column which is date-time -type: vd$Vdate <- as_datetime(vd$V1) :
enter image description here
Then I'm trying to convert the vd-data into time series: vd.ts<- ts(vd, frequency = 365)
But then the dates are gone:
enter image description here
I just cannot get it what I am doing wrong! Could someone help me, please.
Your dates are gone because you need to build the ts dataframe from your variables (V1, ... V7) disregarding the date field and your ts command will order R to structure the dates.
Also, I noticed that you have what is seems like hourly data, so you need to provide the frequency that is appropriate to your time not 365. Considering what you posted your frequency seems to be a bit odd. I recommend finding a way to establish the frequency correctly. For example, if I have hourly data for 365 days of the year then I have a frequency of 365.25*24 (0.25 for the leap years).
So the following is just as an example, it still won't work properly with what I see (it is limited view of your dataset so I am not sure 100%)
# Build ts data (univariate)
vs.ts <- ts(vd$V1, frequency = 365, start = c(2019, 4)
# check to see if it is structured correctly
print(vd.ts, calendar = T)
Finally my time series is working properly. I used
ts <- zoo(measurements, date_times)
and I found out that the date_times was supposed to be converted with as_datetime() as otherwise they were character type. The measurements are converted into data.frame type.

Convert date in vector to Excel date number (R)

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.

Convert dates from XLS into R

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

R - Numeric to Date gives wrong value

I have a data frame DP with a column variable in numeric format which is a numeric representation of Date.
Example: 43282 corresponds to 7/1/2018 (try in excel).
But in R when I call as.Date() to convert it to date, I get the wrong date
DP$Time <- as.Date(DP$variable)
variable Time
1 43282 2088-07-02
What am I doing wrong here?
If it is based on excel, then change the origin from default 1970-01-01 to 1899-12-30
as.Date(43282, origin = '1899-12-30')
#[1] "2018-07-01"
Excel's origin of date is "1899-12-30" and R's origin of date is "1970-01-01". Since they have different origins for the date, while importing the data from Excel, you are getting a different date in R.
Specify the right origin and it will print the right values:
DP$Time <- as.Date(DP$variable, origin = "1899-12-30")

Resources