if (FALSE) {
#Create an ida data frame from the iris data
idf <- as.ida.data.frame(iris)
#Define a function that computes the mean value for every column of a data frame x
#except the index column.
#It returns a data frame with the value of the index column and the mean values.
columnMeans<- function(x, index) {
cbind(index=x[1,match(index, names(x))],
as.data.frame(as.list(apply(x[,names(x) != index],2,mean))))}
#Apply the columnMeans function to the subsets of the iris data identified by the Species column
resSig <- list(Species="VARCHAR(12)", MSepalLength="DOUBLE", MSepalWidth="DOUBLE",
MPetalLength="DOUBLE", MPetalWidth="DOUBLE")
resDf <-
idaTApply(idf, "Species", FUN=columnMeans, output.name="IRIS_MEANS", output.signature=resSig)
#It is possible as well to apply an anonymous function.
#The value "5" of the second parameter designates the position of the "Species" column
#in the idf ida.data.frame.
#The output table of the previous call is recreated because of the "clear.existing=T" parameter.
resDf <- idaTApply(idf, 5,
FUN=function(x, index) {
cbind(index=x[1,match(index, names(x))],
as.data.frame(as.list(apply(x[,names(x) != index],2,mean))))},
output.name="IRIS_MEANS", output.signature=resSig, clear.existing=T)
#Apply the columnMeans2 function which has an additional parameter "columns"
#to specify the columns for which the mean values are computed
columnMeans2 <- function(x, index, columns) {
cbind(index=x[1,match(index, names(x))],
as.data.frame(as.list(apply(x[,names(x) != index & names(x) %in% columns],2,mean))))}
petalColumns <- c("PetalLength", "PetalWidth")
resSig2 <- list(Species="VARCHAR(12)", MPetalLength="DOUBLE", MPetalWidth="DOUBLE")
resDf2 <- idaTApply(idf, "Species", FUN=columnMeans2, output.name="IRIS_MEANS2",
output.signature=resSig2, clear.existing=T, columns=petalColumns)
}
Run the code above in your browser using DataLab