R/as.data.frame.folder.R

Defines functions as.data.frame.folder

Documented in as.data.frame.folder

as.data.frame.folder <- function(x, row.names = NULL, optional = FALSE, ..., group.name = "group") {
  
  fold <- x
  name.fold <- deparse(substitute(x))
  
  # Check of the arguments
  if (!is.folder(fold))
    stop(paste(name.fold, "is not of class 'folder'."))

  # The grouping variable (given by the names of the elements of the folder)
  g <- names(fold)
  
  # Building of the data frame (the grouping variable on the last column):
  # - the first group
  x <- data.frame(fold[[1]], group = g[1], stringsAsFactors = TRUE)
  # - and the next groups
  if (length(g) > 1) {
    for (n in 2:length(g)) {
      x <- rbind(x, data.frame(fold[[n]], group = g[n], stringsAsFactors = TRUE))
    }
  }
  colnames(x)[ncol(x)] <- group.name
  
  return(x)
}

Try the dad package in your browser

Any scripts or data that you put into this service are public.

dad documentation built on Aug. 30, 2023, 5:06 p.m.