Distance between signals / rows for each levels in R -
i have following sample:
id = c(1, 1,3,3,3) long = c("60.466681", "60.664116", "60.766690", "60.86879", "60.986569" ) lat = c("24.158253", "24.266036", "24.368283", "24.479058", "24.5599858") data = data.frame(id, long, lat) data$long <- as.numeric(as.character(data$long)) data$lat <- as.numeric(as.character(data$lat)) data$id <- as.factor(data$id)
i create new column called distance
cumulative sum of distance between each lat/long points each level of factor id
i tried create own function found on internet , using distcosine() package geosphere
. got:
create function
distance <- vectorize(function(i, j) distcosine(data[i,], data[j,]))
run function
library(dplyr) dist <- data %>% filter(id != 0) %>% #i keep because in final data can have id==0 group_by(id) %>% do(distance(.$lat, .$long))
it looks function not work, guess there problem loop go next row.
what wrong?
you try:
data %>% group_by(id) %>% mutate(longlead = lead(long), latlead = lead(lat)) %>% na.omit() %>% rowwise() %>% mutate(dist = distcosine(c(long,lat), c(longlead, latlead)))
which gives:
#source: local data frame [3 x 6] #groups: <by row> # # id long lat longlead latlead dist # (fctr) (dbl) (dbl) (dbl) (dbl) (dbl) #1 1 60.46668 24.15825 60.66412 24.26604 23361.55 #2 3 60.76669 24.36828 60.86879 24.47906 16098.39 #3 3 60.86879 24.47906 60.98657 24.55999 14948.38
the idea here create 2 new columns (longlead
, latlead
) using lead()
, filter out na
values (as last entry in group , therefore, have no subsequent values compute distance against) , perform row-wise operation distcosine()
Comments
Post a Comment