Skip to content

Commit

Permalink
Add panel_data coercion methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-long committed Feb 16, 2019
1 parent 0b441e3 commit 9cd9d7d
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions R/panel_methods.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#' @export

as_pdata.frame <- function(x) {

if (!requireNamespace("plm")) {
stop_wrap("You must have the plm package to convert to pdata.frame.")
}

pdata.frame(unpanel(x), index = c(get_id(x), get_wave(x)))

}

#' @rdname panel_data
#' @export
as_panel_data <- function(x, ...) {
UseMethod("as_panel_data")
}

#' @rdname panel_data
#' @export
as_panel_data.default <- function(x, id = id, wave = wave, ...) {
panel_data(x, id = !! id, wave = !! wave, ...)
}

#' @rdname panel_data
#' @export
as_panel_data.pdata.frame <- function(x, ...) {
indices <- plm::index(x)
id <- names(indices)[1]
wave <- names(indices)[2]
if (id %nin% names(x)) {
x[id] <- indices[id]
}
if (wave %nin% names(x)) {
x[wave] <- indices[wave]
}
panel_data(x, id = !! id, wave = !! wave, ...)
}

#' @rdname panel_data
#' @export
as_panel <- as_panel_data

#' as_panel_data.tsibble <- function(x, ...) {
#'
#' }

1 comment on commit 9cd9d7d

@jacob-long
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helps with #9

Please sign in to comment.