This functions tries to convert any R object into a data.frame object. If x is already a data.frame, it is returned as is. If it is a list or a vector it is converted by means of as.data.frame(). If of any other type, a conversion into an object of class xts is attempted by means of try.xts() and if successful the xts object is converted into a data frame with a variable time containing times as POSIXct and the remaining data columns with the time series data. In this conversion row names are stripped.

try_data_frame(x, time.resolution = "month", as.numeric = FALSE,
  col.names = NULL)

try_tibble(x, time.resolution = "month", as.numeric = FALSE,
  col.names = NULL)

Arguments

x

An R object

time.resolution

character The time unit to which the returned time values will be rounded.

as.numeric

logical If TRUE convert time to numeric, expressed as fractional calendar years.

col.names

character vector

Value

A tibble::tibble object, derived from data.frame.

Note

This function can be used to easily convert time series data into a format that can be easily plotted with package ggplot2. try_tibble is another name for try_data_frame which tracks the separation and re-naming of data_frame into tibble::tibble in the imported packages.

Warning!

The time zone was set to "UTC" by try.xts() in the test cases I used. Setting TZ to "UTC" can cause some trouble as several frequently used functions have as default the local or system TZ and will apply a conversion before printing or plotting time data, which in addition is affected by summer/winter time transitions. This should be taken into account as even for yearly data when conversion is to POSIXct a day (1st of January) will be set, but then shifted some hours if printed on a TZ different from "UTC". I recommend reading the documentation of package lubridate-package where the irregularities of time data and the difficulties they cause are very well described. In many cases when working with time series with yearly observations it is best to work with numeric values for years.

Examples

library(xts)
#> Loading required package: zoo
#> #> Attaching package: 'zoo'
#> The following objects are masked from 'package:base': #> #> as.Date, as.Date.numeric
class(lynx)
#> [1] "ts"
try_data_frame(lynx)
#> time lynx #> 1 1820-02-01 269 #> 2 1821-02-01 321 #> 3 1822-02-01 585 #> 4 1823-02-01 871 #> 5 1824-02-01 1475 #> 6 1825-02-01 2821 #> 7 1826-02-01 3928 #> 8 1827-02-01 5943 #> 9 1828-02-01 4950 #> 10 1829-02-01 2577 #> 11 1830-02-01 523 #> 12 1831-02-01 98 #> 13 1832-02-01 184 #> 14 1833-02-01 279 #> 15 1834-02-01 409 #> 16 1835-02-01 2285 #> 17 1836-02-01 2685 #> 18 1837-02-01 3409 #> 19 1838-02-01 1824 #> 20 1839-02-01 409 #> 21 1840-02-01 151 #> 22 1841-02-01 45 #> 23 1842-02-01 68 #> 24 1843-02-01 213 #> 25 1844-02-01 546 #> 26 1845-02-01 1033 #> 27 1846-02-01 2129 #> 28 1847-02-01 2536 #> 29 1848-02-01 957 #> 30 1849-02-01 361 #> 31 1850-02-01 377 #> 32 1851-02-01 225 #> 33 1852-02-01 360 #> 34 1853-02-01 731 #> 35 1854-02-01 1638 #> 36 1855-02-01 2725 #> 37 1856-02-01 2871 #> 38 1857-02-01 2119 #> 39 1858-02-01 684 #> 40 1859-02-01 299 #> 41 1860-02-01 236 #> 42 1861-02-01 245 #> 43 1862-02-01 552 #> 44 1863-02-01 1623 #> 45 1864-02-01 3311 #> 46 1865-02-01 6721 #> 47 1866-02-01 4254 #> 48 1867-02-01 687 #> 49 1868-02-01 255 #> 50 1869-02-01 473 #> 51 1870-02-01 358 #> 52 1871-02-01 784 #> 53 1872-02-01 1594 #> 54 1873-02-01 1676 #> 55 1874-02-01 2251 #> 56 1875-02-01 1426 #> 57 1876-02-01 756 #> 58 1877-02-01 299 #> 59 1878-02-01 201 #> 60 1879-02-01 229 #> 61 1880-02-01 469 #> 62 1881-02-01 736 #> 63 1882-02-01 2042 #> 64 1883-02-01 2811 #> 65 1884-02-01 4431 #> 66 1885-02-01 2511 #> 67 1886-02-01 389 #> 68 1887-02-01 73 #> 69 1888-02-01 39 #> 70 1889-02-01 49 #> 71 1890-02-01 59 #> 72 1891-02-01 188 #> 73 1892-02-01 377 #> 74 1893-02-01 1292 #> 75 1894-02-01 4031 #> 76 1895-02-01 3495 #> 77 1896-02-01 587 #> 78 1897-02-01 105 #> 79 1898-02-01 153 #> 80 1899-02-01 387 #> 81 1900-02-01 758 #> 82 1901-02-01 1307 #> 83 1902-02-01 3465 #> 84 1903-02-01 6991 #> 85 1904-02-01 6313 #> 86 1905-02-01 3794 #> 87 1906-02-01 1836 #> 88 1907-02-01 345 #> 89 1908-02-01 382 #> 90 1909-02-01 808 #> 91 1910-02-01 1388 #> 92 1911-02-01 2713 #> 93 1912-02-01 3800 #> 94 1913-02-01 3091 #> 95 1914-02-01 2985 #> 96 1915-02-01 3790 #> 97 1916-02-01 674 #> 98 1917-02-01 81 #> 99 1918-02-01 80 #> 100 1919-02-01 108 #> 101 1920-02-01 229 #> 102 1921-02-01 399 #> 103 1922-02-01 1132 #> 104 1923-02-01 2432 #> 105 1924-02-01 3574 #> 106 1925-02-01 2935 #> 107 1926-02-01 1537 #> 108 1927-02-01 529 #> 109 1928-02-01 485 #> 110 1929-02-01 662 #> 111 1930-02-01 1000 #> 112 1931-02-01 1590 #> 113 1932-02-01 2657 #> 114 1933-02-01 3396
try_data_frame(lynx, "year")
#> time lynx #> 1 1821-01-01 269 #> 2 1822-01-01 321 #> 3 1823-01-01 585 #> 4 1824-01-01 871 #> 5 1825-01-01 1475 #> 6 1826-01-01 2821 #> 7 1827-01-01 3928 #> 8 1828-01-01 5943 #> 9 1829-01-01 4950 #> 10 1830-01-01 2577 #> 11 1831-01-01 523 #> 12 1832-01-01 98 #> 13 1833-01-01 184 #> 14 1834-01-01 279 #> 15 1835-01-01 409 #> 16 1836-01-01 2285 #> 17 1837-01-01 2685 #> 18 1838-01-01 3409 #> 19 1839-01-01 1824 #> 20 1840-01-01 409 #> 21 1841-01-01 151 #> 22 1842-01-01 45 #> 23 1843-01-01 68 #> 24 1844-01-01 213 #> 25 1845-01-01 546 #> 26 1846-01-01 1033 #> 27 1847-01-01 2129 #> 28 1848-01-01 2536 #> 29 1849-01-01 957 #> 30 1850-01-01 361 #> 31 1851-01-01 377 #> 32 1852-01-01 225 #> 33 1853-01-01 360 #> 34 1854-01-01 731 #> 35 1855-01-01 1638 #> 36 1856-01-01 2725 #> 37 1857-01-01 2871 #> 38 1858-01-01 2119 #> 39 1859-01-01 684 #> 40 1860-01-01 299 #> 41 1861-01-01 236 #> 42 1862-01-01 245 #> 43 1863-01-01 552 #> 44 1864-01-01 1623 #> 45 1865-01-01 3311 #> 46 1866-01-01 6721 #> 47 1867-01-01 4254 #> 48 1868-01-01 687 #> 49 1869-01-01 255 #> 50 1870-01-01 473 #> 51 1871-01-01 358 #> 52 1872-01-01 784 #> 53 1873-01-01 1594 #> 54 1874-01-01 1676 #> 55 1875-01-01 2251 #> 56 1876-01-01 1426 #> 57 1877-01-01 756 #> 58 1878-01-01 299 #> 59 1879-01-01 201 #> 60 1880-01-01 229 #> 61 1881-01-01 469 #> 62 1882-01-01 736 #> 63 1883-01-01 2042 #> 64 1884-01-01 2811 #> 65 1885-01-01 4431 #> 66 1886-01-01 2511 #> 67 1887-01-01 389 #> 68 1888-01-01 73 #> 69 1889-01-01 39 #> 70 1890-01-01 49 #> 71 1891-01-01 59 #> 72 1892-01-01 188 #> 73 1893-01-01 377 #> 74 1894-01-01 1292 #> 75 1895-01-01 4031 #> 76 1896-01-01 3495 #> 77 1897-01-01 587 #> 78 1898-01-01 105 #> 79 1899-01-01 153 #> 80 1900-01-01 387 #> 81 1901-01-01 758 #> 82 1902-01-01 1307 #> 83 1903-01-01 3465 #> 84 1904-01-01 6991 #> 85 1905-01-01 6313 #> 86 1906-01-01 3794 #> 87 1907-01-01 1836 #> 88 1908-01-01 345 #> 89 1909-01-01 382 #> 90 1910-01-01 808 #> 91 1911-01-01 1388 #> 92 1912-01-01 2713 #> 93 1913-01-01 3800 #> 94 1914-01-01 3091 #> 95 1915-01-01 2985 #> 96 1916-01-01 3790 #> 97 1917-01-01 674 #> 98 1918-01-01 81 #> 99 1919-01-01 80 #> 100 1920-01-01 108 #> 101 1921-01-01 229 #> 102 1922-01-01 399 #> 103 1923-01-01 1132 #> 104 1924-01-01 2432 #> 105 1925-01-01 3574 #> 106 1926-01-01 2935 #> 107 1927-01-01 1537 #> 108 1928-01-01 529 #> 109 1929-01-01 485 #> 110 1930-01-01 662 #> 111 1931-01-01 1000 #> 112 1932-01-01 1590 #> 113 1933-01-01 2657 #> 114 1934-01-01 3396
class(austres)
#> [1] "ts"
try_data_frame(austres)
#> time austres #> 1 1971-04-01 13067.3 #> 2 1971-07-01 13130.5 #> 3 1971-10-01 13198.4 #> 4 1972-01-01 13254.2 #> 5 1972-04-01 13303.7 #> 6 1972-07-01 13353.9 #> 7 1972-10-01 13409.3 #> 8 1973-01-01 13459.2 #> 9 1973-04-01 13504.5 #> 10 1973-07-01 13552.6 #> 11 1973-10-01 13614.3 #> 12 1974-01-01 13669.5 #> 13 1974-04-01 13722.6 #> 14 1974-07-01 13772.1 #> 15 1974-10-01 13832.0 #> 16 1975-01-01 13862.6 #> 17 1975-04-01 13893.0 #> 18 1975-07-01 13926.8 #> 19 1975-10-01 13968.9 #> 20 1976-01-01 14004.7 #> 21 1976-04-01 14033.1 #> 22 1976-07-01 14066.0 #> 23 1976-10-01 14110.1 #> 24 1977-01-01 14155.6 #> 25 1977-04-01 14192.2 #> 26 1977-07-01 14231.7 #> 27 1977-10-01 14281.5 #> 28 1978-01-01 14330.3 #> 29 1978-04-01 14359.3 #> 30 1978-07-01 14396.6 #> 31 1978-10-01 14430.8 #> 32 1979-01-01 14478.4 #> 33 1979-04-01 14515.7 #> 34 1979-07-01 14554.9 #> 35 1979-10-01 14602.5 #> 36 1980-01-01 14646.4 #> 37 1980-04-01 14695.4 #> 38 1980-07-01 14746.6 #> 39 1980-10-01 14807.4 #> 40 1981-01-01 14874.4 #> 41 1981-04-01 14923.3 #> 42 1981-07-01 14988.7 #> 43 1981-10-01 15054.1 #> 44 1982-01-01 15121.7 #> 45 1982-04-01 15184.2 #> 46 1982-07-01 15239.3 #> 47 1982-10-01 15288.9 #> 48 1983-01-01 15346.2 #> 49 1983-04-01 15393.5 #> 50 1983-07-01 15439.0 #> 51 1983-10-01 15483.5 #> 52 1984-01-01 15531.5 #> 53 1984-04-01 15579.4 #> 54 1984-07-01 15628.5 #> 55 1984-10-01 15677.3 #> 56 1985-01-01 15736.7 #> 57 1985-04-01 15788.3 #> 58 1985-07-01 15839.7 #> 59 1985-10-01 15900.6 #> 60 1986-01-01 15961.5 #> 61 1986-04-01 16018.3 #> 62 1986-07-01 16076.9 #> 63 1986-10-01 16139.0 #> 64 1987-01-01 16203.0 #> 65 1987-04-01 16263.3 #> 66 1987-07-01 16327.9 #> 67 1987-10-01 16398.9 #> 68 1988-01-01 16478.3 #> 69 1988-04-01 16538.2 #> 70 1988-07-01 16621.6 #> 71 1988-10-01 16697.0 #> 72 1989-01-01 16777.2 #> 73 1989-04-01 16833.1 #> 74 1989-07-01 16891.6 #> 75 1989-10-01 16956.8 #> 76 1990-01-01 17026.3 #> 77 1990-04-01 17085.4 #> 78 1990-07-01 17106.9 #> 79 1990-10-01 17169.4 #> 80 1991-01-01 17239.4 #> 81 1991-04-01 17292.0 #> 82 1991-07-01 17354.2 #> 83 1991-10-01 17414.2 #> 84 1992-01-01 17447.3 #> 85 1992-04-01 17482.6 #> 86 1992-07-01 17526.0 #> 87 1992-10-01 17568.7 #> 88 1993-01-01 17627.1 #> 89 1993-04-01 17661.5
try_data_frame(austres, "quarter")
#> time austres #> 1 1971-04-01 13067.3 #> 2 1971-07-01 13130.5 #> 3 1971-10-01 13198.4 #> 4 1972-01-01 13254.2 #> 5 1972-04-01 13303.7 #> 6 1972-07-01 13353.9 #> 7 1972-10-01 13409.3 #> 8 1973-01-01 13459.2 #> 9 1973-04-01 13504.5 #> 10 1973-07-01 13552.6 #> 11 1973-10-01 13614.3 #> 12 1974-01-01 13669.5 #> 13 1974-04-01 13722.6 #> 14 1974-07-01 13772.1 #> 15 1974-10-01 13832.0 #> 16 1975-01-01 13862.6 #> 17 1975-04-01 13893.0 #> 18 1975-07-01 13926.8 #> 19 1975-10-01 13968.9 #> 20 1976-01-01 14004.7 #> 21 1976-04-01 14033.1 #> 22 1976-07-01 14066.0 #> 23 1976-10-01 14110.1 #> 24 1977-01-01 14155.6 #> 25 1977-04-01 14192.2 #> 26 1977-07-01 14231.7 #> 27 1977-10-01 14281.5 #> 28 1978-01-01 14330.3 #> 29 1978-04-01 14359.3 #> 30 1978-07-01 14396.6 #> 31 1978-10-01 14430.8 #> 32 1979-01-01 14478.4 #> 33 1979-04-01 14515.7 #> 34 1979-07-01 14554.9 #> 35 1979-10-01 14602.5 #> 36 1980-01-01 14646.4 #> 37 1980-04-01 14695.4 #> 38 1980-07-01 14746.6 #> 39 1980-10-01 14807.4 #> 40 1981-01-01 14874.4 #> 41 1981-04-01 14923.3 #> 42 1981-07-01 14988.7 #> 43 1981-10-01 15054.1 #> 44 1982-01-01 15121.7 #> 45 1982-04-01 15184.2 #> 46 1982-07-01 15239.3 #> 47 1982-10-01 15288.9 #> 48 1983-01-01 15346.2 #> 49 1983-04-01 15393.5 #> 50 1983-07-01 15439.0 #> 51 1983-10-01 15483.5 #> 52 1984-01-01 15531.5 #> 53 1984-04-01 15579.4 #> 54 1984-07-01 15628.5 #> 55 1984-10-01 15677.3 #> 56 1985-01-01 15736.7 #> 57 1985-04-01 15788.3 #> 58 1985-07-01 15839.7 #> 59 1985-10-01 15900.6 #> 60 1986-01-01 15961.5 #> 61 1986-04-01 16018.3 #> 62 1986-07-01 16076.9 #> 63 1986-10-01 16139.0 #> 64 1987-01-01 16203.0 #> 65 1987-04-01 16263.3 #> 66 1987-07-01 16327.9 #> 67 1987-10-01 16398.9 #> 68 1988-01-01 16478.3 #> 69 1988-04-01 16538.2 #> 70 1988-07-01 16621.6 #> 71 1988-10-01 16697.0 #> 72 1989-01-01 16777.2 #> 73 1989-04-01 16833.1 #> 74 1989-07-01 16891.6 #> 75 1989-10-01 16956.8 #> 76 1990-01-01 17026.3 #> 77 1990-04-01 17085.4 #> 78 1990-07-01 17106.9 #> 79 1990-10-01 17169.4 #> 80 1991-01-01 17239.4 #> 81 1991-04-01 17292.0 #> 82 1991-07-01 17354.2 #> 83 1991-10-01 17414.2 #> 84 1992-01-01 17447.3 #> 85 1992-04-01 17482.6 #> 86 1992-07-01 17526.0 #> 87 1992-10-01 17568.7 #> 88 1993-01-01 17627.1 #> 89 1993-04-01 17661.5
class(cars)
#> [1] "data.frame"
try_data_frame(cars)
#> speed dist #> 1 4 2 #> 2 4 10 #> 3 7 4 #> 4 7 22 #> 5 8 16 #> 6 9 10 #> 7 10 18 #> 8 10 26 #> 9 10 34 #> 10 11 17 #> 11 11 28 #> 12 12 14 #> 13 12 20 #> 14 12 24 #> 15 12 28 #> 16 13 26 #> 17 13 34 #> 18 13 34 #> 19 13 46 #> 20 14 26 #> 21 14 36 #> 22 14 60 #> 23 14 80 #> 24 15 20 #> 25 15 26 #> 26 15 54 #> 27 16 32 #> 28 16 40 #> 29 17 32 #> 30 17 40 #> 31 17 50 #> 32 18 42 #> 33 18 56 #> 34 18 76 #> 35 18 84 #> 36 19 36 #> 37 19 46 #> 38 19 68 #> 39 20 32 #> 40 20 48 #> 41 20 52 #> 42 20 56 #> 43 20 64 #> 44 22 66 #> 45 23 54 #> 46 24 70 #> 47 24 92 #> 48 24 93 #> 49 24 120 #> 50 25 85