I am working with this dataset with a date format similar to input below, how do I apply date object to the list such that the first 4 digits are extracted and converted to the date object similar to the output below.
input = {
{"date_start"},
{19900201},
{19900201},
{19980201},
{20010201}
}
output = {date_start, 1990, 1990, 1998, 2001}
eric3
(Eric Rimbey)
January 20, 2023, 11:22pm
2
input = {{“date_start”}, {19900201}, {19900201}, {19980201}, {20010201}};
expectedResult = {“date_start”, 1990, 1990, 1998, 2001};
One very literal approach would be:
result = MapAt[Quotient[#, 10000] &, Flatten@input, 2 ;;]
But you mentioned “dateobject”. Are you actually wanting DateObjects?
MapAt[DateObject[{Quotient[#, 10000], Quotient[Mod[#, 10000], 100], Mod[#, 100]}] &, Flatten@input, 2 ;;]
Both options are actually helpful considering that I am trying to use the complete data for time series and other analysis as well, thanks!
Another way using string matching
input // Flatten //
MapAt[ToString/*(DateObject[{#, {"Year", "", "Month", "", "Day"}}, "Day"] &), 2 ;;]