R/Convert_table.R

Defines functions ConvertTable

Documented in ConvertTable

#' Code level convert table (internal function)
#'
#' @param klass_data  Klass data frame to convert
#' @param code Name of variable containing code (Default = "code")
#' @param parentcode Name of variable of parent code (Default = "parentCode")
#' @param level Name of variable containing level (Default = "level")
#'
#' @return A dataframe
ConvertTable <- function(klass_data, code = "code", parentcode = "parentCode", level= "level"){
    levelset  <- as.numeric(max(klass_data[,level]))
    uni <- unique(klass_data[klass_data[, level]  == levelset, code])
    dx <- matrix(NA, length(uni), levelset)
    dx <- as.data.frame(dx)
    dx[, levelset] <- as.character(uni)
    for (i in 1:(levelset-1)){
      m <- match(dx[,levelset - i + 1], klass_data[, code])
      dx[, levelset-i] <- as.character(klass_data[, parentcode][m])
    }
    names(dx) <- paste("level", 1:levelset, sep="")
    return(dx)
}

Try the klassR package in your browser

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

klassR documentation built on March 30, 2021, 5:12 p.m.