R/td.recode.R

Defines functions td.recode

Documented in td.recode

td.recode <- function(tdf, col, oTable, oDatabase = "", codes, other = "same") {
    if (!is.td.data.frame(tdf)) 
        stop("'tdf' is not a td data frame")
    if (length(codes)%%2 != 0) 
        stop("Length of 'codes' is not an even number")
    obj <- .td.object(attr(tdf, "tableName"), attr(tdf, "database"))
    wc <- ""
    if (!is.null(attr(tdf, "whereClause"))) 
        wc <- paste(" WHERE ", attr(tdf, "whereClause"))
    oObj <- .td.object(oTable, oDatabase)
    nm <- names(tdf)
    if (!col %in% nm) 
        stop("'col' to transform is not a column in the td data frame.")
    nm <- nm[-(grep(col, nm))]
    nms <- paste(rep(gettextf("\"%s\"", nm)), collapse = ",")
    recodeText <- .td.genrecode(col, codes, as = col, other)
    nms <- paste(nms, ",", recodeText, collapse = ",")
    query <- gettextf("CREATE TABLE %s AS (SELECT %s FROM %s %s) WITH DATA", oObj, nms, obj, wc)
    if (.td.objectExists(oObj)) 
        stop(gettextf("Table %s already exists.", oObj))
    df <- try(tdQueryUpdate(query))
    if (length(df) == 1L && df == "No Data") 
        return(td.data.frame(oTable, oDatabase))
    stop(gettextf("Error: %s", paste(df, collapse = "")))
}
 
nonsleepr/teradataR documentation built on May 23, 2019, 9:32 p.m.