0

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

www
  • 38,575
  • 12
  • 48
  • 84

1 Answers1

0

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))
               })
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • Hi Parfait, this output works perfectly, however I am now having issues writing the data to a csv, where I wasn't previously. The deldirList writes as a 'LargeList' where as when i'm only working with small data sets it just saves as a 'List'. In order to write the List to a CSV i am just using the function write.csv(deldirList$delshs,"Output.csv") This works with a normal list, but when using a large list, the output is just blank. Is there a different way to write large lists to csvs? – Benjamin Moss Jul 28 '16 at 16:52
  • As mentioned, `deldirList` is a list of individual `deldir` objects. So this is a nested list. You can save individual items to file by adding `write.csv()` inside the `lapply()` loop or you can run `deldirAll <- do.call(rbind, deldirList); write.csv(deldirAll, "Output.csv")` to append all lists together. – Parfait Jul 28 '16 at 18:46
  • How did it go? I do not have the package to test fully? Try a `dput(deldirList)` in you post to test other solutions. – Parfait Jul 29 '16 at 20:01