1

After applying hexbin'ning I would like to know which id or rownumbers of the original data ended up in which bin.

I am currently analysing spatial data and I am binning, e.g., depth of water and temperature. Ideally, I would like to map the colormap of the bins back to the spatial map to see where more or less common parameter combinations exist. I'm not bound to hexbin though. I wasn't able to figure out from the documentation, how to trace which datapoint ends up in which bin. It seems hexbin() only stores counts.

Is there a function that generates a list with one entry for every bin, each containing a vector of all rownumbers that were assigned to that bin?

Please point me into the right direction.

Up to now, I use plain hexbin to do the binning:

library(hexbin)
set.seed(5)
df <- data.frame(depth=runif(1000,min=0,max=100),temp=runif(1000,min=4,max=14))
h <- hexbin(df)

but currently I see no way to extract rownames of df from h that link the bins to df. Possibly there is no such thing, maybe I overlooked it or there is a completely different approach needed.

smci
  • 32,567
  • 20
  • 113
  • 146
Janhoo
  • 597
  • 5
  • 21
  • How are you currently doing binning? Please edit your question to include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and the code you are currently running. This will make it much easier to help you. – MrFlick Nov 26 '14 at 17:31

1 Answers1

2

Assuming you are using the hexbin package, then you will need to set IDs=TRUE to be able to go back to the original rows

library(hexbin)
set.seed(5)
df <- data.frame(depth=runif(1000,min=0,max=100),temp=runif(1000,min=4,max=14))
h<-hexbin(df, IDs=TRUE)

Then to get the bin number for each observation, you can use

h@cID

To get the count of observations in the cell populated by a particular observation, you would do

h@count[match(h@cID, h@cell)]

The idea is that the second observation df[2,] is in cell h@cID[2]=424. Cell 424 is at index which(h@cell==424)=241 in the list of cells (zero count cells appear to be omitted). The number of observations in that cell is h@count[241]=2.

MrFlick
  • 195,160
  • 17
  • 277
  • 295