Gragbow
Gragbow

Reputation: 177

Aggregate function not allowing to aggregate using maximum function

I have a fairly basic question here but I can't for the life of me work out why it isn't working.

I have this simple code

month = sample(1:12, 5000, replace= TRUE)
year = sample(2000:2019,5000, replace = TRUE)
loss = round(rlnorm(5000,7,2))
claim.data = cbind(month, year, loss)
head(claim.data)

max.year = aggregate(loss~year, data = claim, FUN = max)

but it is throwing up the error

Error in get(as.character(FUN), mode = "function", envir = envir) : object 'FUN' of mode 'function' was not found

which suggests that max function doesn't work. I have tried various other functions such as mean and min and they both work fine.

Upvotes: 3

Views: 934

Answers (1)

akrun
akrun

Reputation: 887691

It would be an issue if the function name is already assigned to another object

max <- 25
aggregate(loss~year, data = claim.data, FUN = max)

Error in get(as.character(FUN), mode = "function", envir = envir) :
object 'FUN' of mode 'function' was not found

Either use anonymous function call

aggregate(loss~year, data = claim.data, FUN = function(x) max(x))
#   year    loss
#1  2000  143866
#2  2001  316487
#3  2002  186172
#4  2003  465357
#5  2004  233817
#6  2005  847532
#7  2006  294599
#8  2007  382048
#9  2008 1768795
#10 2009  686856
#11 2010 1356117
#12 2011 2093160
#13 2012  411778
#14 2013 2002641
#15 2014  289696
#16 2015  220853
#17 2016  428111
#18 2017  362979
#19 2018  335988
#20 2019  178828

Or apply on a fresh R session

Upvotes: 2

Related Questions