Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion of date format %B %Y

Tags:

date

r

Can we somehow convert dates such as "November 2017", "December 2017" to date? I tried to import csv data, but received factor columns.

I tried the following code, but was not successful.

as.POSIXct(as.character(dat$Date), format = "%B %Y")
like image 712
Geron Avatar asked Dec 11 '22 07:12

Geron


1 Answers

A POSIXct date needs the day of the month to be complete and valid.
You can add it to the date strings, then use the format "%B %Y %d" e.g. :

as.POSIXct(paste(as.character(dat$Date),"01"), format = "%B %Y %d")

BTW, when you import a csv you can set stringsAsFactors=FALSE (as argument of read.csv/read.table functions) to obtain characters instead of factors.

like image 194
digEmAll Avatar answered Jan 05 '23 15:01

digEmAll