Separate date from a column and store in a new column

73 views Asked by At

I am trying to separate date from one column and trying to put the date in another column but the new column is horribly wrong when I compare the dates in both columns. I am attaching pic below. I am separating date from the started_at column and storing it in the date column.

The code I tried is in the image. For example in the 1st date in the started_at column is "4/1/2019" but when i separated it in a new column then it is returning "0004-01-20".

I have tried using format() as well as as.Date() but it is also not giving the same value as in parent column started_at. Picture is attached below.

1

There are 1 answers

1
Ignacio2424 On

I'm not sure why the format is not working, but as string variable, you could just:

stringsplit(all_trips$started_at, " " )

Or with tidyr:

all_trips <- tidyr::separate(all_trips, started_at, c("date", "hour"), sep = " ")

In stringr:

all_trips[c("date", "hour")] <- str_split_fixed(all_trips$started_at," ", 2)