0

I want to extract only the month and year of this output, may i know how do you do that using R?

op_date_2 <- as.Date(data$op_date, "%d-%b-%y")

[1] "1992-01-01" "1992-01-02" "1992-01-02" "1992-01-02" "1992-01-02" "1992-01-02" "1992-01-03" "1992-01-05" "1992-01-07" "1992-01-09"
[11] "1992-01-09" "1992-01-10" "1992-01-10" "1992-01-10" "1992-01-14" "1992-01-14" "1992-01-16" "1992-01-16" "1992-01-16" "1992-01-21"
[21] "1992-01-21" "1992-01-23" "1992-01-28" "1992-01-30" "1992-01-30" "1992-01-30" "1992-02-01" "1992-02-01" "1992-02-02" "1992-02-04"
yarnabrina
  • 1,561
  • 1
  • 10
  • 30
Jeri Lim
  • 55
  • 1
  • 8
  • 3
    `format(op_date_2, "%m-%Y")` ? – Ronak Shah Sep 16 '19 at 02:18
  • 1
    It's not clear how your example code relates to the example output? The output is `%Y-%m-%d`, the code uses `%d-%b-%y`. It would help to include some or all of `data`, using plain text, in your question. – neilfws Sep 16 '19 at 02:18
  • @RonakShah For many reasons, it would generally be desirable to put the year followed by the month. – Tim Biegeleisen Sep 16 '19 at 02:19

1 Answers1

1

You can use the lubridate library. First transform your data and then retrieve month and year.

library(lubridate)
Test <- c("1992-01-01", "1992-01-02", "1994-03-02")
TestData <- as.POSIXlt(Test, format="%Y-%m-%d")
YourMonths <- month(TestData)
YourMonths
YourYears <- year(TestData)
YourYears
Orlando Sabogal
  • 1,470
  • 7
  • 20