convert xpt DateTime into as.POSIX - r

I have a xpt data frame that I read into RStudio with a DateTimePOSIX column that looks like this:
ID DateTime
1 2017-01-06T06:02
1 2017-01-30T08:02
2 2017-01-30T15:02
How can I convert the DateTime column into DateTimePOSIX?
I need the as.POSIXct formatted DateTime for further calculations of time differences etc.

Related

Retrieving the format of an object of class 'Date' in R

Say I want to store 1 February, 2003 as a date using a day-month-year without century format and I do the following:
> date <- as.Date("01-02-03", format = "%d-%m-%y")
And then I forget what format that date was in, and do this:
> date
[1] "2003-02-01"
In order to determine if 01 is the month or the day, I can do this:
> format(date, "%d")
[1] "01"
But is there a way to directly retrieve the format this date was stored in?
R only stores dates Date objects in one format. And technically it's a numeric format. The value is only formatted to look like a date when you print() the value. There is no formatting stored with the Date object. For example
dput(as.Date("2003-02-01"))
# structure(12084, class = "Date")
Dates are always printed yyyy-mm-dd ISO style when using the default print method. You can change that by using format() or strftime(). But the internal representation is always the same.

Excel datetime format to R for date and time of day

I have a data frame df that has a column datetime with datetimes from Excel of the format e.g. 41653.34 corresponding to 1/14/2014 8:12:00.
I can convert it to YYYY-MM-DD using:
df$datetime <- as.Date(df$datetime, origin = "1899-12-30")
However, this loses the information about the time of day of the raw data. How can I convert this data to a date and time instead of just a date?
Thank you.

Datatime in PYSPARK

I have the time as the following 103400 with string type I need to convert it into 10:34:00 in a time format using pyspark only.
suppose that the name of the data frame is u and the name of the column is hhmm_utente

convert time dd/mm/yyyy to number in R

I have a data frame with a time column, the format is like dd/mm/yyyy and I want to
convert dd/mm/yyyy to a number mmyyyy and store it in a new column
then create another two new columns for mm and yyyy separately and create a new data frame with my original data frame and these 2 columns
How can I do it? Thanks!
If the time column is not character you can use:
format(timecolumn,"%m%Y")
format(timecolumn,"%m")
format(timecolumn,"%Y")
If it is character:
paste0(substr(timecolumn,4,5),substr(timecolumn,7,10))
substr(timecolumn,4,5)
substr(timecolumn,7,10)

Cast string to Date in R from a CSV

Format a date from CSV into a date R can use.
I have a time series data file. I want to load it into R, and cast the Date column to be something usable by R. Can I specify the input format to as.Date(), or use another function that will correctly cast a Date such as 1/1/14?
In other languages I'm used to passing in a format string that tells the caster exactly how to format it, e.g. toDate('1/1/1', '%d/%m/%y'). I haven't found this function yet for R.
my_time_series.csv
Date Value
1/1/14 123
1/2/14 128.56
1/3/14 129.14
1/4/14 130.13
1/5/14 137.97
1/6/14 141.05
1/7/14 141.35
1/8/14 142.14
1/9/14 142.14
1/10/14 149.89
Now I can import it into R:
$ R
> dat = read.csv("time series test.csv", header = TRUE)
> dat
Date Value
1 1/1/14 123.0000
2 1/2/14 128.5693
3 1/3/14 129.1474
4 1/4/14 130.1361
5 1/5/14 137.9758
6 1/6/14 141.0548
7 1/7/14 141.3517
8 1/8/14 142.1449
9 1/9/14 142.1479
10 1/10/14 149.8912
Ok now I need to format those dates as actual dates. The as.Date() casting function looks promising, but returns an incorrect date:
> as.Date('1/10/14')
[1] "0001-10-14"
So I searched for whether I can specify an input format for as.Date(), but it only has a second parameter format for the output format.
I tried to work around this in Excel before saving the CSV, but it doesn't have any formats that seem to work by default with as.Date().
I'm an idiot, was just typing in the wrong formatter. This works fine.
> as.Date('1/10/14', '%m/%d/%y')
[1] "2014-01-10"
So if I have any arbitrary date format in the future, just rearrange the format string.

Resources