[R] R 3.1.2 using a custom function in aggregate() function on Windows 7 OS 64bit
Typhenn Brichieri-Colombi
typhenn at yahoo.com
Wed Mar 4 23:02:06 CET 2015
Hello,
I am trying to use the following custom function in an aggregatefunction, but cannot get R to recognize my data. I’ve read the help on function()and on aggregate() but am unable to solve my problem. How can I get R torecognize the data inputs for the custom function nested within aggregate()?
My custom function is found below, as well as the errormessage I get when I run it on a test data set (I will be using this functionon a much larger dataset (over 600,000 rows))
Thank you for your time and your help!
d_rule<-function(a,x){
i<-which(a==max(a))
out<-ifelse(length(i)==1, x[i], min(x))
return(out)
}
a<-c(2,2,1,4,2,5,2,3,4,4)
x<-c(1:10)
g<-c(1,1,2,2,3,3,4,4,5,5)
dat<-as.data.frame(cbind(x,g))
test<-aggregate(dat, by=list(g), FUN=d_rule,dat$a, dat$x)
Error in dat$x : $ operator is invalid for atomic vectors
[[alternative HTML version deleted]]
More information about the R-help
mailing list