title: Pairwise combine the rows of a grouped R dataframe and apply functions to each pair
body: <p>I sampled the ultrasound calling activity of bats with a group of automated, time-synced, and stationary detector microphones. In the resulting dataframe, every row is a recorded call, together with the recording time, the length of the call (a proxy for activity), and the x- and y-coordinate of the device.</p>
  
  <p>I want to correlate detector distance with delta, the call length difference between each two detectors to assess the influence of spatial distance on detector performance. For this, I need to pairwise combine every dataframe row with all others, and this per timestamp (= group), finally apply two functions computing (i) delta and (ii) the spatial distance between the detectors of each pair. Here is a sample dataframe, consisting of three devices and three timestamps.</p>
  
  <pre><code>bats &lt;- data.frame(time=as.POSIXct(rep(c("2019-05-18 00:00:00","2019-05-18 00:05:00","2019-05-18 00:10:00"),each=3)), device=rep(c("a","b","c"),3), pos.X=rep(c(0,2,4),3), pos.Y=rep(c(5,7,9),3), call.length=rlnorm(9,meanlog=0.5,sd=1))
  </code></pre>
  
  <p>For one group, I was successful using combn() and raster::pointDistance():</p>
  
  <pre><code>delta &lt;- abs(combn(x=bats$call.length, m=2, FUN=function(x) x[1]-x[2])) # calculate delta
  
  coordinates &lt;- bats[ ,3:4] # make dataframe of coordinates
  
  row.pairs &lt;- combn(x=nrow(bats), m=2) # get indices for row pairs
  
  df_row.pairs &lt;- cbind(x=coordinates[row.pairs[1,],], y=coordinates[row.pairs[2,],]) # make dataframe from the row pairs
  distance &lt;- raster::pointDistance(p1=df_row.pairs[2:1], df_row.pairs[4:3], lonlat=FALSE) # calculate distances among coordinates
  df &lt;- data.frame(delta,distance=distance) # bind distances and deltas to a dataframe
  </code></pre>
  
  <p>But trying to extend the code for all time stamps with dplyr::group_by(time) %>% group_map(), I miserably failed. </p>
  
  <p>Tidyverse solutions are especially welcome, but I am very grateful for every suggestion. </p>