Assuming that you will also apply the formula to D2 and D3, use across() from {dplyr}:
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
result<-structure(list(n = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
28, 29), M1 = c(29L, 1L, 28L, 27L, 25L, 26L, 24L, 20L, 21L,
22L, 23L, 15L, 12L, 17L, 18L, 19L, 16L, 13L, 14L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 4L, 2L, 3L), M2 = c(1, 29, 28, 27, 26, 25,
24, 23, 22, 21, 20, 15, 12, 19, 18, 17, 16, 14, 13, 11, 10, 9,
8, 7, 6, 5, 4, 3, 2), M3 = c(1L, 29L, 28L, 27L, 25L, 26L, 24L,
20L, 21L, 22L, 23L, 15L, 12L, 17L, 18L, 19L, 16L, 13L, 14L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 4L, 2L, 3L)), class = "data.frame", row.names = c(NA,-29L))
dif <- result %>%
mutate(D1 = M1-M2, D2 = M1-M3, D3 = M2-M3)
rho <- function(d) {
1 - (6 * (sum(d^2)) / (length(d) * ((length(d)^2) - 1)))
}
dif %>% summarise(across(D1:D3, ~ rho(.x)))
#> D1 D2 D3
#> 1 0.5778325 0.6137931 0.9640394
Created on 2022-03-05 by the reprex package (v2.0.1)