Can anyone extract these dates?
class(Seatbelts)
#> [1] "mts" "ts"
Seatbelts
#> DriversKilled drivers front rear kms PetrolPrice VanKilled law
#> Jan 1969 107 1687 867 269 9059 0.10297181 12 0
#> Feb 1969 97 1508 825 265 7685 0.10236300 6 0
#> Mar 1969 102 1507 806 319 9963 0.10206249 12 0
...
Seatbelts[,0]
#>
#> Jan 1969
#> Feb 1969
#> Mar 1969
...
rownames(Seatbelts)
#> NULL
row.names(Seatbelts)
#> NULL
row.names(as.matrix(Seatbelts))
#> NULL
I'm stumped. Have tried lots more besides above.
Related but unhelpful:
CodePudding user response:
time
can get the year in decimal format.
time(Seatbelts)
If we need to change the format, then convert to yearmon
class (after checking the frequency
to make sure it is monthly as @Rui Barradas commented)
zoo::as.yearmon(time(Seatbelts))
-output
[1] "Jan 1969" "Feb 1969" "Mar 1969" "Apr 1969" "May 1969" "Jun 1969" "Jul 1969" "Aug 1969" "Sep 1969" "Oct 1969" "Nov 1969"
[12] "Dec 1969" "Jan 1970" "Feb 1970" "Mar 1970" "Apr 1970" "May 1970" "Jun 1970" "Jul 1970" "Aug 1970" "Sep 1970" "Oct 1970"
[23] "Nov 1970" "Dec 1970" "Jan 1971" "Feb 1971" "Mar 1971" "Apr 1971" "May 1971" "Jun 1971" "Jul 1971" "Aug 1971" "Sep 1971"
[34] "Oct 1971" "Nov 1971" "Dec 1971" "Jan 1972" "Feb 1972" "Mar 1972" "Apr 1972" "May 1972" "Jun 1972" "Jul 1972" "Aug 1972"
[45] "Sep 1972" "Oct 1972" "Nov 1972" "Dec 1972" "Jan 1973" "Feb 1973" "Mar 1973" "Apr 1973" "May 1973" "Jun 1973" "Jul 1973"
[56] "Aug 1973" "Sep 1973" "Oct 1973" "Nov 1973" "Dec 1973" "Jan 1974" "Feb 1974" "Mar 1974" "Apr 1974" "May 1974" "Jun 1974"
[67] "Jul 1974" "Aug 1974" "Sep 1974" "Oct 1974" "Nov 1974" "Dec 1974" "Jan 1975" "Feb 1975" "Mar 1975" "Apr 1975" "May 1975"
[78] "Jun 1975" "Jul 1975" "Aug 1975" "Sep 1975" "Oct 1975" "Nov 1975" "Dec 1975" "Jan 1976" "Feb 1976" "Mar 1976" "Apr 1976"
[89] "May 1976" "Jun 1976" "Jul 1976" "Aug 1976" "Sep 1976" "Oct 1976" "Nov 1976" "Dec 1976" "Jan 1977" "Feb 1977" "Mar 1977"
[100] "Apr 1977" "May 1977" "Jun 1977" "Jul 1977" "Aug 1977" "Sep 1977" "Oct 1977" "Nov 1977" "Dec 1977" "Jan 1978" "Feb 1978"
[111] "Mar 1978" "Apr 1978" "May 1978" "Jun 1978" "Jul 1978" "Aug 1978" "Sep 1978" "Oct 1978" "Nov 1978" "Dec 1978" "Jan 1979"
[122] "Feb 1979" "Mar 1979" "Apr 1979" "May 1979" "Jun 1979" "Jul 1979" "Aug 1979" "Sep 1979" "Oct 1979" "Nov 1979" "Dec 1979"
[133] "Jan 1980" "Feb 1980" "Mar 1980" "Apr 1980" "May 1980" "Jun 1980" "Jul 1980" "Aug 1980" "Sep 1980" "Oct 1980" "Nov 1980"
[144] "Dec 1980" "Jan 1981" "Feb 1981" "Mar 1981" "Apr 1981" "May 1981" "Jun 1981" "Jul 1981" "Aug 1981" "Sep 1981" "Oct 1981"
[155] "Nov 1981" "Dec 1981" "Jan 1982" "Feb 1982" "Mar 1982" "Apr 1982" "May 1982" "Jun 1982" "Jul 1982" "Aug 1982" "Sep 1982"
[166] "Oct 1982" "Nov 1982" "Dec 1982" "Jan 1983" "Feb 1983" "Mar 1983" "Apr 1983" "May 1983" "Jun 1983" "Jul 1983" "Aug 1983"
[177] "Sep 1983" "Oct 1983" "Nov 1983" "Dec 1983" "Jan 1984" "Feb 1984" "Mar 1984" "Apr 1984" "May 1984" "Jun 1984" "Jul 1984"
[188] "Aug 1984" "Sep 1984" "Oct 1984" "Nov 1984" "Dec 1984"
CodePudding user response:
You can use the xts function, extract the index and then convert it into a date format:
library(xts)
as.Date(index(as.xts(Seatbelts)), format = "%M %Y")
Output:
[1] "1969-01-01" "1969-02-01" "1969-03-01" "1969-04-01" "1969-05-01" "1969-06-01" "1969-07-01" "1969-08-01"
[9] "1969-09-01" "1969-10-01" "1969-11-01" "1969-12-01" "1970-01-01" "1970-02-01" "1970-03-01" "1970-04-01"
[17] "1970-05-01" "1970-06-01" "1970-07-01" "1970-08-01" "1970-09-01" "1970-10-01" "1970-11-01" "1970-12-01"
[25] "1971-01-01" "1971-02-01" "1971-03-01" "1971-04-01" "1971-05-01" "1971-06-01" "1971-07-01" "1971-08-01"
[33] "1971-09-01" "1971-10-01" "1971-11-01" "1971-12-01" "1972-01-01" "1972-02-01" "1972-03-01" "1972-04-01"
[41] "1972-05-01" "1972-06-01" "1972-07-01" "1972-08-01" "1972-09-01" "1972-10-01" "1972-11-01" "1972-12-01"
[49] "1973-01-01" "1973-02-01" "1973-03-01" "1973-04-01" "1973-05-01" "1973-06-01" "1973-07-01" "1973-08-01"
[57] "1973-09-01" "1973-10-01" "1973-11-01" "1973-12-01" "1974-01-01" "1974-02-01" "1974-03-01" "1974-04-01"
[65] "1974-05-01" "1974-06-01" "1974-07-01" "1974-08-01" "1974-09-01" "1974-10-01" "1974-11-01" "1974-12-01"
[73] "1975-01-01" "1975-02-01" "1975-03-01" "1975-04-01" "1975-05-01" "1975-06-01" "1975-07-01" "1975-08-01"
[81] "1975-09-01" "1975-10-01" "1975-11-01" "1975-12-01" "1976-01-01" "1976-02-01" "1976-03-01" "1976-04-01"
[89] "1976-05-01" "1976-06-01" "1976-07-01" "1976-08-01" "1976-09-01" "1976-10-01" "1976-11-01" "1976-12-01"
[97] "1977-01-01" "1977-02-01" "1977-03-01" "1977-04-01" "1977-05-01" "1977-06-01" "1977-07-01" "1977-08-01"
[105] "1977-09-01" "1977-10-01" "1977-11-01" "1977-12-01" "1978-01-01" "1978-02-01" "1978-03-01" "1978-04-01"
[113] "1978-05-01" "1978-06-01" "1978-07-01" "1978-08-01" "1978-09-01" "1978-10-01" "1978-11-01" "1978-12-01"
[121] "1979-01-01" "1979-02-01" "1979-03-01" "1979-04-01" "1979-05-01" "1979-06-01" "1979-07-01" "1979-08-01"
[129] "1979-09-01" "1979-10-01" "1979-11-01" "1979-12-01" "1980-01-01" "1980-02-01" "1980-03-01" "1980-04-01"
[137] "1980-05-01" "1980-06-01" "1980-07-01" "1980-08-01" "1980-09-01" "1980-10-01" "1980-11-01" "1980-12-01"
[145] "1981-01-01" "1981-02-01" "1981-03-01" "1981-04-01" "1981-05-01" "1981-06-01" "1981-07-01" "1981-08-01"
[153] "1981-09-01" "1981-10-01" "1981-11-01" "1981-12-01" "1982-01-01" "1982-02-01" "1982-03-01" "1982-04-01"
[161] "1982-05-01" "1982-06-01" "1982-07-01" "1982-08-01" "1982-09-01" "1982-10-01" "1982-11-01" "1982-12-01"
[169] "1983-01-01" "1983-02-01" "1983-03-01" "1983-04-01" "1983-05-01" "1983-06-01" "1983-07-01" "1983-08-01"
[177] "1983-09-01" "1983-10-01" "1983-11-01" "1983-12-01" "1984-01-01" "1984-02-01" "1984-03-01" "1984-04-01"
[185] "1984-05-01" "1984-06-01" "1984-07-01" "1984-08-01" "1984-09-01" "1984-10-01" "1984-11-01" "1984-12-01"