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

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