I have this dataset shown below. I want to separate the Trip.Start.Timestamp so it will be in two different columns called StartDate and StartTime. How do I write my codes in R? I have tried to write the codes with POSIXct, but it still didn't work. Thanks,
Asked
Active
Viewed 63 times
-2
-
3When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Don't just post pictures of data please. Also, R doesn't have a primitive data structure for just time, just date+time. So how exactly would you like to store that infromation. – MrFlick Feb 15 '18 at 19:35
1 Answers
0
You can make a reproducible example with only the trip.start.timestamp variable and one other, let's say trip.second.
trip.start.timestamp <- c('01/29/2016 08:00:00 PM','01/11/2016 07:15:00 PM','01/13/2016 08:30:00 AM')
trip.second <- c(240, 960, 360)
df <- data.frame(trip.start.timestamp, trip.second)
To make StartDate and StartTime you can use substr() since the format is always the same.
df$StartDate <- substr(df$trip.start.timestamp, 1, 10)
df$StartTime <- substr(df$trip.start.timestamp, 12, 22)
You get:
df
trip.start.timestamp trip.second StartDate StartTime
01/29/2016 08:00:00 PM 240 01/29/2016 08:00:00 PM
01/11/2016 07:15:00 PM 960 01/11/2016 07:15:00 PM
01/13/2016 08:30:00 AM 360 01/13/2016 08:30:00 AM

jkortner
- 549
- 2
- 8
- 23