Looping through groups with deldir() in R

150 Views Asked by At

I have inputted some data consisting of three columns, X,Y and Group.

I am looking to get the underling data for a voronoi diagram for each group.

By using

a=deldir(Test.data$X,Test.data$Y,rw=c(0,1,0,1))

I succesfully create the voronoi data for the entire dataset. However I do not know how to iterate this process through the different groups that I have in the dataset.

Does anyone have any ideas? I have expereince with the ggplot function and know in here I can simply add a third dimension, something like

ggplot(Test.data,aes(x=X,y=Y,colour=Group))

Is there a way I can get a similar affect with the deldir() function

Thanks in advance for your help.

Ben

1

There are 1 best solutions below

3
On

Consider creating a list of groups and then filter dataset. Below lapply() creates a list of deldir objects, one for each distinct group:

groups <- unique(Test.data$groupcol)

deldirList <- lapply(groups, function(g) {
                       temp <- Test.data[Test.data$groupcol==g,]
                       deldir(temp$X, temp$Y, rw=c(0,1,0,1))
               })