stumped! I have a column of two digit character data that represents months of the year (01, 02, 03, 04.....). I need to convert it to month names (Jan, Feb, Mar....) as chr class

here is the lubridate code (where yrv2#month is my column of two digit month numbers) which results in the following error......

yrv2$month_name <- month(ymd(paste0("2022-", yrv2$month, "-01")), label = FALSE)

Error in month(ymd(paste0("2022-", yrv2$month, "-01")), label = FALSE) :
unused argument (label = FALSE)

what <- "2023-03-07"
have <- lubridate::ymd(what)
want <- "2023-Mar-07"
done <- strftime(have, format = "%Y-%b-%d")
done == want
#> [1] TRUE
done
#> [1] "2023-Mar-07"

Created on 2023-03-07 with reprex v2.0.2

1 Like

It's not completely clear what you want but maybe:

library(data.table)
library(lubridate)

nums  <- c("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12")
dat1 <- data.table(nums = sample(nums, 20, replace = TRUE))


dat1[, newnums := fcase(
  nums == "01", "Jan",
  nums == "02", "Feb",
  nums == "03", "Mar",
  nums == "04", "Apr",
  nums == "05", "May",
  nums == "06", "Jun",
  nums == "07", "Jul",
  nums == "08", "Aug",
  nums == "09", "Sep",
  nums == "10", "Oct",
  nums == "11", "Nov",
  nums == "12", "Dec")
]
dat1[, nd := ymd(paste0("2022-", newnums, "-01" ))]
dat1

You may want to have a look at this FAQ.

1 Like

(some_months = c(1,3,4))
(some_months_as_names <- month.abb[some_months])

(some_month_chars <- c("01","06"))
(some_months_chars_as_names <- month.abb[as.integer(some_month_chars)])

thanks so much....Im new at this but rampong up

I see....thanks so much!

This topic was automatically closed 42 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.