Creating the R Function - Aster R

Teradata Aster® R User GuideUpdate 3

Product
Aster R
Release Number
7.00.02.01
Published
December 2017
Language
English (United States)
Last Update
2018-04-13
dita:mapPath
fop1497542774450.ditamap
dita:ditavalPath
Generic_no_ie_no_tempfilter.ditaval
dita:id
fbp1477004286096
lifecycle
previous
Product Category
Software
  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.