Camilo Mora
2014-Oct-27 05:05 UTC
[R] "inahull” from package alphahull not working when used with apply
Hi everyone, I have a two column (x,y) database with say 20 million rows. I want to check the points that are inside of a hull created with the package alphahull. The function that does this is call ?inahull?, and it runs well when I use it for one point at a time. But when I try to optimize the function using ?apply?, it gives me the wrong results. I wonder what I am doing wrong? Below is the code: I also wonder if "inahull", could be used in data.table? library (alphahull) DT<-data.frame(x=c(0.25,0.75,0.75,0.25),y=c(0.25,0.25,0.75,0.75)) #creates hull HULL<-ahull(DT, alpha=0.5) #generate two points as test TEST<-data.frame(x=c(0.25,0.5),y=c(0.5,0.5)) TEST<-data.matrix(TEST) #check individual points InPoint1<-inahull(HULL, c(TEST[1,1],TEST[1,2])) InPoint2<-inahull(HULL, c(TEST[2,1],TEST[2,2]))> InPoint1[1] FALSE> InPoint2[1] TRUE #repead check using apply in2D=apply(TEST, 1,function(x, y) inahull(HULL, p = TEST))> in2D[1] FALSE FALSE [[alternative HTML version deleted]]
Bart Kastermans
2014-Oct-27 06:03 UTC
[R] "inahull” from package alphahull not working when used with apply
On 27/10/14 06:05, Camilo Mora wrote:> Hi everyone, > > I have a two column (x,y) database with say 20 million rows. I want to check the points that are inside of a hull created with the package alphahull. The function that does this is call ?inahull?, and it runs well when I use it for one point at a time. But when I try to optimize the function using ?apply?, it gives me the wrong results. I wonder what I am doing wrong? Below is the code: I also wonder if "inahull", could be used in data.table? > > library (alphahull) > DT<-data.frame(x=c(0.25,0.75,0.75,0.25),y=c(0.25,0.25,0.75,0.75)) > > #creates hull > HULL<-ahull(DT, alpha=0.5) > > #generate two points as test > TEST<-data.frame(x=c(0.25,0.5),y=c(0.5,0.5)) > TEST<-data.matrix(TEST) > > #check individual points > InPoint1<-inahull(HULL, c(TEST[1,1],TEST[1,2])) > InPoint2<-inahull(HULL, c(TEST[2,1],TEST[2,2])) > >> InPoint1 > [1] FALSE >> InPoint2 > [1] TRUE > > #repead check using apply > in2D=apply(TEST, 1,function(x, y) inahull(HULL, p = TEST))Why do x or y not appear in the function expression? Note that any apply with a function that does not use its argument will give a constant sequence. Now also, apply gives the whole dimension as an argument, so once you try to use y as an argument you will get the error (I tried just to get the error correct): Error: argument "y" is missing, with no default If that wasn't enough yet; let me ask another question, what is wrong with p = TEST? Best, Bart>> in2D > [1] FALSE FALSE > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > R-help at r-project.org mailing list > 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. >