I have the data table dt.data, where the column begTime is a character-column with a special date format. The date format is the following: 01-MON-YY.
This is how my data table looks like:
dt.data <- structure(list(begTime = c("01-JAN-23", "01-JAN-23", "01-JAN-23",
"01-FEB-23", "01-FEB-23", "01-FEB-23", "01-MAR-23", "01-MAR-23",
"01-MAR-23", "01-APR-23", "01-APR-23", "01-APR-23", "01-MAY-23",
"01-MAY-23", "01-MAY-23", "01-JUN-23", "01-JUN-23", "01-JUN-23",
"01-JUL-23", "01-JUL-23", "01-JUL-23", "01-AUG-23", "01-AUG-23",
"01-AUG-23", "01-SEP-23", "01-SEP-23", "01-SEP-23", "01-OCT-23",
"01-OCT-23", "01-OCT-23", "01-NOV-23", "01-NOV-23", "01-NOV-23",
"01-DEC-23", "01-DEC-23", "01-DEC-23"), toBook = c("KG21", "KG64",
NA, "KG21", "KG64", NA, "KG21", "KG64", NA, "KG21", "KG64", NA,
"KG21", "KG64", NA, "KG21", "KG64", NA, "KG21", "KG64", NA, "KG21",
"KG64", NA, "KG21", "KG64", NA, "KG21", "KG64", NA, "KG21", "KG64",
NA, "KG21", "KG64", NA), sumQ = c(1440, 200.947775009365,
-3978.571120032, 0, -17812.0922910065, 19996.296169952, 0, 12995.1445322506,
-15165.299710032, 2160, -53923.0554299322, 54824.803160056, 504,
-7045.79687321736, 8256.75532999996, 0, -14919.1153921838, 13458.1132399999,
0, -15966.4071659115, 15083.67141, 720, -18714.0358020241, 18129.3816599999,
240, 2905.88494461573, -6727.47018, 0, 39413.3865474728, -36206.19812,
0, -9407.5891668633, 9800.53, 0, -17781.1490225234, 17946.678
), unit = c("MWh", "MWh", "MWh", "MWh", "MWh", "MWh", "MWh",
"MWh", "MWh", "MWh", "MWh", "MWh", "MWh", "MWh", "MWh", "MWh",
"MWh", "MWh", "MWh", "MWh", "MWh", "MWh", "MWh", "MWh", "MWh",
"MWh", "MWh", "MWh", "MWh", "MWh", "MWh", "MWh", "MWh", "MWh",
"MWh", "MWh"), sumValue = c(108891.36, 128053.530976637,
-273632.88711977, 0, -1165755.80930466, 1274897.88251747, 0,
512406.636632069, -617755.657019838, 102372.48, -2495006.82184836,
2522765.0072208, 15463.728, -305363.977539244, 336017.268406819,
0, -487578.86709908, 445164.689526293, 0, -517033.916204391,
475791.067501664, 26482.32, -640698.403786333, 612188.309980192,
8319.6, 96201.4521993924, -243811.777077578, 0, 1582012.15513231,
-1467874.96096514, 0, -429850.119024673, 434161.39860219, 0,
-719195.470550453, 724300.2)), row.names = c(NA, -36L), class = c("data.table",
"data.frame"))
Now I need help with the following two questions:
- How can I convert the character-column to a standard R date-column with the date format
01-01-2023? - How can I extract the month and the year of this column to create a new column, which looks like this
JAN2023, ...DEC2023?
seems you are using
data.table:If using tidyverse, you could do: