2016-08-01 5 views

Antwort

0
get_count <- function(x, y, h) { 
    my_dist <- function(x2, y2) { 
    return(sqrt((x - x2)^2 + (y - y2)^2)) 
    } 

    distances <- mapply(my_dist, attr(h, 'xcm'), attr(h, 'ycm')) 

    return(attr(h, 'count')[which.min(distances)]) 
} 
5

Angenommen, Sie verwenden die Hexbin-Funktion von library(hexbin) können Sie die bin-IDs verwenden, um zu erreichen, was Sie wollen.

Rufen Sie die Funktion als hexbin(..., IDs = T) und das Ergebnis wird ein Feld haben, das Ihnen sagt, in welche Bin die Punkte fallen.

Arbeitsbeispiel:

library(hexbin) 

x <- c(1, 1.2, 1, 3, 5, -2 ,1, 0, 0.8) 
y <- c(1, 1, 0, -1, 0, 2, -1, 1, 1) 

h <- hexbin(x, y, xbins = 3,IDs = T) 

#what is the cell ID of point 1? 
ID1 <- [email protected][1] 

#how many points fall in that cell? 
sum([email protected] == ID1) #answer is 4 in this case