Creating a new date column for a large data frame

Dear R Studio Community,

I have a data frame that originally has 15 columns.

The column 'started_at' (third column) contains dates and time in the format DD/MM/YYYY HH:MM. I am trying to create a new column called date that uses the date information from the 'started_at' column. To do this, I use the following code chunk:

all_trips$date <- as.Date(all_trips$started_at)

When I run this in R, the column that is created doesn't extract the correct date from the 'started_at' column. for example, instead of returning '27/11/2021' it returns '0027-11-20'. This is shown in the image above where the new column 'date' is all the way at the bottom and the 'started_at' column is the third column from the top.

What can I do to fix this problem?

Many thanks

1 Like

Thanks for providing code , but you could take further steps to make it more convenient for other forum users to help you.

Share some representative data that will enable your code to run and show the problematic behaviour.

You might use tools such as the library datapasta, or the base function dput() to share a portion of data in code form, i.e. that can be copied from forum and pasted to R session.

using the information from as.Date function - RDocumentation

all_trips$date <- as.Date(all_trips$started_at,  format="%d/%m/%Y")

should fix your problem.

thank you! it worked

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.