Crunch-io / rcrunch

R package for interacting with the Crunch API
https://crunch.io/r/crunch/
GNU Lesser General Public License v3.0
9 stars 15 forks source link

[Feature request] `as.data.frame()` method for `Categories` objects #635

Open rossellhayes opened 7 months ago

rossellhayes commented 7 months ago

Including an as.data.frame() method for Categories objects would make it easier to use join functions to map between the names and numeric codes associated with a variable.

Here are the functions that I'm currently using in my scripts to achieve this:

as.data.frame.Category <- function(category) {
  data <- category@.Data
  names(data) <- category@names
  data[lengths(data) == 0] <- NA
  as.data.frame(data)
}

as.data.frame.Categories <- function(categories) {
  do.call(rbind, lapply(categories@.Data, as.data.frame))
}
gergness commented 7 months ago

Yeah, seems like an oversight because there is one for variables.

A complication I can foresee would be that I don't think the category metadata schema is consistent between variables. Do you think numeric_value or datetime column should always be present, or only when relevant?

rossellhayes commented 4 months ago

I think my expectation would be that an as.data.frame() method would produce a data frame that looks like the print method for Categories objects, i.e. only including the columns that are relevant for that variable type, but I would be happy with either implementation!

1beb commented 3 months ago

I think I have a method for this already in crunchtabs. I think it's in codebook. https://github.com/Crunch-io/crunchtabs/blob/main/R/codeBookSummary.R#L137