File: classes.R

package info (click to toggle)
r-cran-hardhat 1.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,656 kB
  • sloc: sh: 13; makefile: 2
file content (33 lines) | stat: -rw-r--r-- 883 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#' Extract data classes from a data frame or matrix
#'
#' When predicting from a model, it is often important for the `new_data` to
#' have the same classes as the original data used to fit the model.
#' `get_data_classes()` extracts the classes from the original training data.
#'
#' @param data A data frame or matrix.
#'
#' @return
#'
#' A named list. The names are the column names of `data` and the values are
#' character vectors containing the class of that column.
#'
#' @examples
#' get_data_classes(iris)
#'
#' get_data_classes(as.matrix(mtcars))
#'
#' # Unlike .MFclass(), the full class
#' # vector is returned
#' data <- data.frame(col = ordered(c("a", "b")))
#'
#' .MFclass(data$col)
#'
#' get_data_classes(data)
#' @export
get_data_classes <- function(data) {
  ptype <- extract_ptype(data)

  validate_has_unique_column_names(ptype, "data")

  lapply(ptype, class)
}