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.
Usage
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)
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
class(lynx)
#> [1] "ts"
try_tibble(lynx)
#> # A tibble: 114 × 2
#> time x
#> <date> <dbl>
#> 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
#> # ℹ 104 more rows
try_tibble(lynx, as.numeric = TRUE)
#> # A tibble: 114 × 2
#> time x
#> <dbl> <dbl>
#> 1 1821 269
#> 2 1822 321
#> 3 1823 585
#> 4 1824 871
#> 5 1825 1475
#> 6 1826 2821
#> 7 1827 3928
#> 8 1828 5943
#> 9 1829 4950
#> 10 1830 2577
#> # ℹ 104 more rows
try_tibble(lynx, "year")
#> # A tibble: 114 × 2
#> time x
#> <date> <dbl>
#> 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
#> # ℹ 104 more rows
class(austres)
#> [1] "ts"
try_tibble(austres)
#> # A tibble: 89 × 2
#> time x
#> <date> <dbl>
#> 1 1971-04-01 13067.
#> 2 1971-07-01 13130.
#> 3 1971-10-01 13198.
#> 4 1972-01-01 13254.
#> 5 1972-04-01 13304.
#> 6 1972-07-01 13354.
#> 7 1972-10-01 13409.
#> 8 1973-01-01 13459.
#> 9 1973-04-01 13504.
#> 10 1973-07-01 13553.
#> # ℹ 79 more rows
try_tibble(austres, as.numeric = TRUE)
#> # A tibble: 89 × 2
#> time x
#> <dbl> <dbl>
#> 1 1971. 13067.
#> 2 1971. 13130.
#> 3 1972. 13198.
#> 4 1972 13254.
#> 5 1972. 13304.
#> 6 1972. 13354.
#> 7 1973. 13409.
#> 8 1973 13459.
#> 9 1973. 13504.
#> 10 1973. 13553.
#> # ℹ 79 more rows
try_tibble(austres, "quarter")
#> # A tibble: 89 × 2
#> time x
#> <date> <dbl>
#> 1 1971-04-01 13067.
#> 2 1971-07-01 13130.
#> 3 1971-10-01 13198.
#> 4 1972-01-01 13254.
#> 5 1972-04-01 13304.
#> 6 1972-07-01 13354.
#> 7 1972-10-01 13409.
#> 8 1973-01-01 13459.
#> 9 1973-04-01 13504.
#> 10 1973-07-01 13553.
#> # ℹ 79 more rows
class(cars)
#> [1] "data.frame"
try_tibble(cars)
#> # A tibble: 50 × 2
#> speed dist
#> <dbl> <dbl>
#> 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
#> # ℹ 40 more rows