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
#Invent data
DF <- data.frame(Country = c("Afgan", "Bang", "Barb", "belize", "China",
"DR", "Ghana", "Guat", "Kenya", "Yemen"),
A = round(runif(n = 10, min = 50, max = 100)),
B = round(runif(n = 10, min = 50, max = 100)),
C = round(runif(n = 10, min = 50, max = 100)),
D = round(runif(n = 10, min = 50, max = 100)))
DF
#> Country A B C D
#> 1 Afgan 51 79 98 66
#> 2 Bang 92 60 75 71
#> 3 Barb 72 77 80 98
#> 4 belize 83 95 72 55
#> 5 China 52 100 94 74
#> 6 DR 74 65 78 98
#> 7 Ghana 70 58 65 89
#> 8 Guat 82 73 76 54
#> 9 Kenya 78 81 61 84
#> 10 Yemen 94 96 56 89
#Make a column to label the pairs of rows
DF <- DF |> mutate(Group = rep(LETTERS[1:5], each = 2))
DF
#> Country A B C D Group
#> 1 Afgan 51 79 98 66 A
#> 2 Bang 92 60 75 71 A
#> 3 Barb 72 77 80 98 B
#> 4 belize 83 95 72 55 B
#> 5 China 52 100 94 74 C
#> 6 DR 74 65 78 98 C
#> 7 Ghana 70 58 65 89 D
#> 8 Guat 82 73 76 54 D
#> 9 Kenya 78 81 61 84 E
#> 10 Yemen 94 96 56 89 E
#Compute the averages of the groups
Averages <- DF |> group_by(Group) |>
summarize(across(.cols = -Country, .fns = mean))
Averages
#> # A tibble: 5 x 5
#> Group A B C D
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 A 71.5 69.5 86.5 68.5
#> 2 B 77.5 86 76 76.5
#> 3 C 63 82.5 86 86
#> 4 D 76 65.5 70.5 71.5
#> 5 E 86 88.5 58.5 86.5