7.00.02.01 - Creating the R Function - Aster R

Teradata Aster® R User GuideUpdate 3

prodname
Aster R
vrm_release
7.00.02.01
created_date
December 2017
category
Programming Reference
User Guide
featnum
B700-1033-700K
  1. Define the R function.
    WeeklyUpdate <- function(x) {
    profit <- x[,"salesprice"] - x[,"cost"]
    i <- data.frame(x[,"date"], x[,"orderid"], x[,"itemid"], x[,"cost"], x[,"salesprice"], profit)
    DailySales<-aggregate(x[,"salesprice"]~x[,"date"], i, sum)
    DailyProfit<-aggregate(profit~x[,"date"], i, sum)
    
    w <- data.frame(DailySales[,1], DailySales[,2], DailyProfit[,2])
    
    colnames(w)<-c("Date","Total Sales", "Total Profit")
    return(w)
    }
  2. Create a table in the database based on the input data, and convert it to a virtual data frame.
    ta.create(Fruit_sales, 
    table="fruit_tmp", 
    schemaName="public", 
    tableType="dimension", 
    row.names=FALSE, 
    colTypes=NULL 
    )
    
    tadf.fruit <- ta.data.frame("fruit_tmp")
  3. Use the Aster R function aa.apply() to run the function created in Step 1, on the virtual data frame "tadf.fruit" created in Step 2.
    aa.apply(tadf.fruit,
             FUN = "WeeklyUpdate", 
             MARGIN = c(), 
             out.format=list(columns=c("Date", "Total Sales", "Total Profit"), 
                             columnTypes=c("date", "numeric", "numeric")))
    
          Date Total Sales Total Profit
    1 5/1/2010       29.80         5.65
    2 5/2/2010       31.78         6.80
    3 5/3/2010       19.25         4.72
    4 5/4/2010       20.57         5.72
    For details about the arguments and options of the function aa.apply(), refer to the inline help.