separate DateTime column in R - r

I have a datetime column in a data frame (named data) like this:
data$Date_Time
##[1] 14JUN2011:09:45:00
##[2] 15JUN2011:10:45:00
##[3] 16JUN2011:11:35:09
I'd like to obtain the date and time into separate columns.
For Date, it is easy to use:
data$Date <- as.Date(data$Date_Time, format("%d%b%Y"))
But I am having trouble to get the Time column.

You probably want to use the POSIXt class for the time, which can be parsed like:
data$Time <- strptime(data$Date_Time, "%d%b%Y:%H:%M:%S")
Also, I think that the date parsing should be:
data$Date <- as.Date(data$Date_Time, format="%d%b%Y")
In other words, don't call the format function. You're just lucky that works anyway.

you may use substr(date$Date_Time, 11, 18)

Related

How to fix timestamp in character class to correct format in R?

I am having trouble converting timestamps in character class. I am trying to use following function
df$Time <- strptime(df$Time, format = "%H:%M:%S")
Then problem I have is that my Time column (currently class Character) has values listed like this 1:00:05 or 55:42. There are no dates in the column.
While the 55:45 is in mm:ss, the 1:00:05 is in hh:mm:ss. Could this be the problem? I can't think of anything else. When I run the above function, I keep getting NA for the whole column. How can I resolve this.
Thanks for any help. I have been stuck and have tried so many different functions such as hms from lubridate and as.POSIXct
Here is sample dataset
df <- data.frame(Time = c("1:00:05", "55:45","34:33","1:12:35"))
df$Time <- as.character(df$Time)
head(df)
We may use parse_date_time
library(lubridate)
parse_date_time(df$Time, c("HMS", "MS"))

R: How to fill in values in a new column using the values of another column

I have a dataset in R with a column called event_date.
The variables look like this:
31-Dec-18
30-Dec-18
28-Dec-18
And so on.
I want to create a new column called date where I separate out the day of the event. So it looks like:
31
30
28
I'm pretty new to working with R, so I'm wondering whether a for loop is the way to go, or if there's a more efficient way I don't know about.
if the dates are of type character
df$date <- sub(".*-.*-(.*)","\\1", df$event_date)
otherwise you can look into creating data type objects in R.
If the days are two digit, then substr would be faster
df$day <- substr(df$event_date, 1, 2)
Or convert to Date class and extract the day
df$day <- format(as.Date(df$event_date, "%d-%b-%y"), "%d")

How to sort 'mm-yyyy' format in R?

Date
01-2018
02-2018
01-2019
02-2019
I tried using arrange(df, Date)
It gets arranged as
01-2018
01-2019
02-2018
02-2019
Here is one base R option. We can try ordering the data frame using an on the fly date based on the text strings.
df <- data.frame(Date=c("01-2018", "02-2018", "01-2019", "02-2019"),
stringsAsFactors=FALSE)
df[order(as.Date(paste0("01-", df$Date), format="%d-%m-%Y")), ]
[1] "01-2018" "02-2018" "01-2019" "02-2019"
Note that I form a complete date by arbitrarily using the first of the month, for each text date, using as.Date with the correct format mask to generate a bona fide date.
For best results, consider storing your dates in a proper date column, or, if you must use text, use an ISO format which would at least sort properly.

Formatting and converting column into datetime format in R?

I am fairly new to R and need help with applying operations to an entire column in a dataframe. Imagine a few values of the date_time column in the df look like this:
date_time
2017-05-01T00:00:00.000Z
2017-05-01T10:00:00.000Z
2017-05-01T20:00:00.000Z
...
Currently date_time is of type factor. If everything was formatted nicely, I think I want to do something similar to what I have below to convert it to DateTime (based off of what I've been seeing online):
df$date_time <- strptime(x = as.character(df$date_time), format = "%Y-%m-%d %H:%M:%S")
Does this look correct?
Assuming the code above is correct for converting the factor into DateTime, we still need to do some formatting for that to work. In order to use the code above, I have to get rid of the T that separates the date and time and replace that with ' ', and cut off the .000Z at the end. How can I do this for the entire column?
Thanks!

Having difficulty with the start argument for ts( ). Losing date formatting

First, new to programming.
I built a table with 3 columns and I want to evaluate based on time series, so I'm playing around with the ts() function. The first column of my table is DATE as.date in the format "yyyy-mm-dd". I have one observation per variable per day. I've apply ts() to the table and tried start=1 (first observation?) and checked head(df) and the DATE column is sending back loose sequence of numbers that I can't identify (12591, 12592, 12593, 12594, 12597, 12598).
Could it be that the as.date is messing things up?
The line I use is:
ts(dy2, start=1, frequency= 1)
I've also been playing with the deltat argument. In the help file it suggests 1/12 for monthly data. Naturally, I tried 1/365 (for daily data), but have yet to be successful.
As suggested by G. Grothendieck you can use the zoo package. Try this:
require(zoo)
dates <- as.Date(dy2[,1], format = "%Y-%m-%d")
x1 <- zoo(dy2[,2], dates)
plot(x2)
x2 <- zoo(dy2[,3], dates)
plot(x1)
If this does not work, please provide further details about your data as requested by MrFlick. For example, print the output of dput(dy2) or at least head(dy2).

Resources