Kristen Cyr
Kristen Cyr

Reputation: 726

Converting a column of integers that aren't in date format already into abbreviated months

I'm trying to convert a column of full integers into date format of abbreviated months. The column has numbers like : 01 02 04 15 13. etc. I want these numbers to show the month they correspond to. Could someone please tell me how. the code I'm trying is this:

#Changing integers to Month Abbrev.
dets_per_month$monthcollected = as.POSIXlt(dets_per_month$monthcollected, format = "%m", origin = "%m")

but I realize the column doesn't have an origin because it's not in date format.

Upvotes: 0

Views: 62

Answers (2)

dyrland
dyrland

Reputation: 648

I would recommend the lubridate package for all things date-time related. It's a nifty package and has more utility than base R, but YMMV.

library(lubridate)
x <- rep(1:12, 2)
lubridate::month(x, label=TRUE)

Upvotes: 0

SmokeyShakers
SmokeyShakers

Reputation: 3412

month.abb[as.integer(dets_per_month$monthcollected)]

Upvotes: 2

Related Questions