Newbie101
Newbie101

Reputation: 1

How do I split the date and time into 2 columns using R programming?

The column looks like this:

reported_time
3/9/2019 7:07
24/09/2019 08:17:17
3/9/2019 7:27
20/10/2019 08:02:20
2/5/2019 14:15
2/5/2019 14:49
2/5/2019 20:22
20/10/2019 20:42:15
18/09/2019 15:19:20
9/10/2019 9:11
21/10/2019 08:37:05
2/5/2019 8:57
2/5/2019 12:35
2/5/2019 22:46

Tried the followings, somehow it doesn't work

df$Date <- as.Date(df$reported_time)

Error in df$reported_time : object of type 'closure' is not subsettable

df$Time <- format(df$reported_time,"%H:%M:%S")

Error in df$reported_time : object of type 'closure' is not subsettable

data$reported_time <- as.POSIXct(as.character(data$reported_time), format =  "%Y%m%d%H%M")

Error in data$reported_time : object of type 'closure' is not subsettable

Another column by the header name "acknowledged_on" consists of the same date, time format.

Tried the followings, but failed.

Will appreciate advice from anyone. Thank you

df$Date <- lapply(strsplit(as.character(df$acknowledged_on), " "), "[", 1)

Error in df$acknowledged_on : object of type 'closure' is not subsettable

df$Time <- lapply(strsplit(as.character(df$acknowledged_on), " "), "[", 2)

Error in df$acknowledged_on : object of type 'closure' is not subsettable

df$Date <- lapply(strsplit(as.character(df$acknowledged_on), " "), "[", 1)]

Error: unexpected ']' in "df$Date <- lapply(strsplit(as.character(df$acknowledged_on), " "), "[", 1)]"

df$Time <- lapply(strsplit(as.character(df$acknowledged_on), " "), "[", 2)]

Error: unexpected ']' in "df$Time <- lapply(strsplit(as.character(df$acknowledged_on), " "), "[", 2)]"

df$Date <- as.Date(df$acknowledged_on) #already got this one from the answers above

Error in df$acknowledged_on : object of type 'closure' is not subsettable

df$Time <- format(as.POSIXct(df$acknowledged_on) ,format = "%H:%M")

Error in df$acknowledged_on : object of type 'closure' is not subsettable

df$Date <- as.Date(df$acknowledged_on)

Error in df$acknowledged_on : object of type 'closure' is not subsettable

df$Time <- format(df$acknowledged_on,"%H:%M:%S")

Error in df$acknowledged_on : object of type 'closure' is not subsettable

desired output is (the seconds are removed from the time) :

reported_time 3/9/2019 7:07 24/09/2019 08:17 3/9/2019 7:27 20/10/2019 08:02 2/5/2019 14:15 2/5/2019 14:49 2/5/2019 20:22 20/10/2019 20:42 18/09/2019 15:19 9/10/2019 9:11 21/10/2019 08:37 2/5/2019 8:57 2/5/2019 12:35 2/5/2019 22:46

Upvotes: 0

Views: 69

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 101034

I have no idea how is the expected output should look like. Below is my solution:

Assuming you have the time report as a string s such as

s <- "3/9/2019 7:07 24/09/2019 08:17:17 3/9/2019 7:27 20/10/2019 08:02:20 2/5/2019 14:15 2/5/2019 14:49 2/5/2019 20:22 20/10/2019 20:42:15 18/09/2019 15:19:20 9/10/2019 9:11 21/10/2019 08:37:05 2/5/2019 8:57 2/5/2019 12:35 2/5/2019 22:46"

then you can produce a data.frame as below, including Date and Time:

v <- gsub("(.*):\\d{2}","\\1",sapply(unlist(regmatches(s,gregexpr("([0-9/]+)(\\s)([0-9:/]+)",s))),function(x) format(as.POSIXct(x,tz="",tryFormats = c("%d/%m/%Y %H:%M"), "%d/%m/%Y %H:%M")),USE.NAMES = F))
df <- data.frame(reported_time = v, Date = gsub("([0-9/]+)(\\s.*)","\\1",v), Time = gsub("(.*\\s)([0-9:/]+)","\\2",v))

where the output looks like:

> df
      reported_time       Date  Time
1  2019-09-03 07:07 2019-09-03 07:07
2  2019-09-24 08:17 2019-09-24 08:17
3  2019-09-03 07:27 2019-09-03 07:27
4  2019-10-20 08:02 2019-10-20 08:02
5  2019-05-02 14:15 2019-05-02 14:15
6  2019-05-02 14:49 2019-05-02 14:49
7  2019-05-02 20:22 2019-05-02 20:22
8  2019-10-20 20:42 2019-10-20 20:42
9  2019-09-18 15:19 2019-09-18 15:19
10 2019-10-09 09:11 2019-10-09 09:11
11 2019-10-21 08:37 2019-10-21 08:37
12 2019-05-02 08:57 2019-05-02 08:57
13 2019-05-02 12:35 2019-05-02 12:35
14 2019-05-02 22:46 2019-05-02 22:46

Upvotes: 1

Related Questions