0
$\begingroup$

I have a column in dataframe which should represent the date, named "taken", and looks like this:

taken

Sat Jan 01 15:30:27 CET 2011

Thu Jan 06 00:54:26 CET 2011

Sat Jan 08 19:49:02 CET 2011

Sun Jan 16 09:57:16 CET 2011

etc (more than 30000 rows)

Now, I need to change class from factor to date, but I haven't found solution for this kind of format. Since it would be easier with date looking like this:

taken

Jan 01 2011

Jan 06 2011

Jan 08 2011

Jan 16 2011

...How could I drop some words from a string (obviously, Mon/Tue... time and CET in this case) to change it into date?

$\endgroup$
1
$\begingroup$

I wouldn't use them as time at first place. What would I do:

String <- "Sat Jan 01 15:30:27 CET 2011"

As your dates have same number of elements in them we could just count what part of the String we would like to get.

date <- substr(String, start = 5, stop = 10)

year <- substr(String, start =25 , stop = 28)

Attach them togheter

final <- paste(date,year, sep = " ")

as.Date(final, format = "%b %d %Y")

Little bit more information about as.Date https://www.statmethods.net/input/dates.html I hope it helps you :)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.