数字年份但月份作为字符。如何将月份更改为数字?

Numeric year but month as character. How to change months into numeric?

所以假设我有一个数据集,其中“日期”列包含 2011-2017 年和每年的月份,但是月份是用字母写的。例如:

日期:1 月 11 日

我想将月份设为数字,这样我得到:

日期:11-01

关于如何解决这个问题有什么建议吗?

亲切的问候!

输入正确的日期,解析它们,然后格式化它们。

x <- c("11-Jan", "12-Feb")
Sys.setlocale("LC_TIME", "C") #parsing of months depends on locale
format(
  as.Date(paste0(x, "-1"), format = "%y-%b-%d"),
  "%y-%m"
)
#[1] "11-01" "12-02"

有关格式字符串的详细信息,请参阅 help("strptime")

假设您的数据如下:

df1 <- structure(list(day_mon = c("16-Dec", "18-Nov", "12-Oct", "8-Oct", 
"15-May", "29-Jun", "22-Feb", "25-May", "23-Jan", "24-Oct", "23-May", 
"27-Sep", "9-Apr", "28-Oct", "18-Jan", "8-Apr", "7-Jan", "13-Dec", 
"28-Nov", "24-May"), year = c(2012L, 2014L, 2011L, 2015L, 2015L, 
2015L, 2011L, 2015L, 2012L, 2015L, 2011L, 2012L, 2014L, 2012L, 
2013L, 2011L, 2017L, 2016L, 2014L, 2014L)), 
row.names = c(
   1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
   13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L), class = "data.frame")

您可以:

# Format the month and day: mon_day_fmt => character vector
df1$mon_day_fmt <- paste(
   sprintf(
      "%02d",
      match(
         gsub(
            "\d+\-(\w+)",
            "\1",
            with(
               df1,
               day_mon
            )
         ),
         month.abb
      )
   ),
   sprintf(
      "%02d",
      as.integer(
         gsub(
            "^(\d+)\-\w+$",
            "\1",
            with(
               df1,
               day_mon
            )
         )
      )
   ),
   sep = "-"
)

# Create a date vector: date => Date Vector
df1$date <- as.Date(
   paste(
      df1$year,
      df1$mon_day_fmt,
      sep = "-"
   )
)