Learn R Programming

ggpmisc (version 0.2.8)

try_data_frame: Convert an R object to a data frame

Description

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 succesful 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 = "second", as.numeric = FALSE)

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.

Value

  • A dataframe.

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 Jannuary) will be set, but then shifted some hours if printed on a TZ different from "UTC". I recommend reading the documentation of package lubridate 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

Run this code
library(xts)
try_data_frame(lynx)
try_data_frame(lynx, "year")
try_data_frame(austres)
try_data_frame(austres, "quarter")
try_data_frame(cars)

Run the code above in your browser using DataLab