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.
Assuming you are using the
hexbin
package, then you will need to setIDs=TRUE
to be able to go back to the original rowsThen to get the bin number for each observation, you can use
To get the count of observations in the cell populated by a particular observation, you would do
The idea is that the second observation
df[2,]
is in cellh@cID[2]=424
. Cell 424 is at indexwhich(h@cell==424)=241
in the list of cells (zero count cells appear to be omitted). The number of observations in that cell ish@count[241]=2
.