Problem in aggregate function implementation in R

r
aggregation
basic
function

#1

can some one explain me why these two works :slight_smile:

aggregate(subset(iris,select=-Species),subset(iris,select=Species),mean)

aggregate(iris[-5],iris[5],mean)

but not this :slight_smile:

aggregate(iris[,-5],iris[,5],mean)


#2

@vijaypalmanit
The aggregate function accepts data-frames as its first parameter. I think that might be the reason the second code is not getting executed.
`

a<-train[1]
b<-train[,1]
class(a)
[1] "data.frame"
class(b)
[1] “integer”`

iris[,-5] is of class integer and iris[5] is of class data.frame

Hope this helped.


#3

yes Correct ! :+1:


#4

Not exactly. Try this and see. Here, the second parameter is a list.

 aggregate(iris[,-5], list(iris[,5]), mean)

`


#5

Hi,

@r_achar is correct

typeof(iris[5])
list
typeof(iris[,5])
integer

The second one is coming as integer because of the way data has been stored in the dataframe

sapply(iris,typeof)
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
    "double"     "double"     "double"     "double"    "integer"

Now, if we run this command to convert the factors to characters in the data frame

iris[5] <- as.character(iris[5])
sapply(iris,typeof)
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
    "double"     "double"     "double"     "double"    "character"

This is still not solving the problem as we want the data type to be list

iris[5] <- list(iris[5])
sapply(iris,typeof)
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
    "double"     "double"     "double"     "double"    "list"

Now run the command, it will work :slight_smile:

aggregate(iris[,-5], iris[,5], mean)
 Species Sepal.Length Sepal.Width Petal.Length Petal.Width
1     setosa        5.006       3.428        1.462       0.246
2 versicolor        5.936       2.770        4.260       1.326
3  virginica        6.588       2.974        5.552       2.026

Hope this helps

Regards,
Anant


#6

Nice Explanation…:slight_smile: