[R] R 3.1.2 using a custom function in aggregate() function on Windows 7 OS 64bit

Bert Gunter gunter.berton at gene.com
Thu Mar 5 06:15:27 CET 2015


What do you think dat$a is?

I recommend that you spend some time with an R tutorial if you plan to
use R. Your code is pretty bad. Examples: use of the ifelse
construction instead of if ... else; return()

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll




On Wed, Mar 4, 2015 at 2:02 PM, Typhenn Brichieri-Colombi via R-help
<r-help at r-project.org> wrote:
> 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]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list