Q:

how to get average of each row based on two or several naming

I am trying to figure out how to get the mean of each several samples against each several samples 
For example, if I have a data like this 
x <- data.frame(grp = rep(c("a", "b"), each = 3),
+                 z = c(4.36e-3, 5.72e-3, 4.17e-3, 2.85e-2, 3.37e-2, 3.27e-2))

I can get the mean of the first three a and the second three b like this 
library(dplyr)
y <- df %>%
  group_by(grp) %>%
  summarise(
    log_gmean = mean(z))

But I have a more complicated data as follows:
df<- structure(list(X = structure(1:16, .Label = c("A", "AA", "AAA", 
"AAAA", "AAAAA", "AAAAAA", "AAAAAAA", "B", "BB", "BBB", "BBBB", 
"BBBBB", "BBBBBB", "BBBBBBB", "BBBBBBBB", "BBBBBBBBB"), class = "factor"), 
    Abin1 = c(1287.262207, 1306.222656, 1313.836426, 1348.760986, 
    1380.257568, 1480.120728, 1546.444946, 1593.406494, 1609.530151, 
    1659.934326, 1680.896118, 1771.564087, 1838.789185, 1954.952271, 
    2029.988647, 2161.85498), Abin2 = c(0, 1640.227417, 2546.210205, 
    763.8786621, 648.7984619, 777.3566284, 1483.07959, 1687.942017, 
    633.1990356, 2816.612305, 1519.554077, 1439.477905, 1390.946899, 
    770.6799927, 3678.585449, 10349.13403), Abin3 = c(0, 1383.959839, 
    733.5137939, 1580.105225, 1483.07959, 2726.177979, 5145.451172, 
    1599.141724, 1599.141724, 3808.109619, 1835.313721, 1456.654907, 
    1400.979736, 0, 2367.459229, 15896.90771), UNIM1 = c(0, 1640.227417, 
    1640.227417, 681.8775024, 1483.07959, 779.6516113, 5145.451172, 
    2291.312744, 2291.312744, 2671.817627, 0, 1525.63916, 1890.740479, 
    1552.753662, 4848.474609, 21809.00244), UNIM2 = c(1480.188843, 
    1784.711304, 1969.312378, 1590.000732, 1240.046021, 2688.334961, 
    6260.527832, 1829.667969, 1568.855835, 4382.629395, 0, 2519.726563, 
    647.5497437, 1234.635132, 3500.700439, 2461.044922), UNIM3 = c(697.336853, 
    1478.803467, 2124.638672, 1353.765381, 1401.213379, 2741.994141, 
    3740.750244, 1474.960083, 0, 3826.848389, 1348.323975, 2414.01709, 
    1550.477661, 1585.388428, 4546.097168, 10992.08936)), .Names = c("X", 
"Abin1", "Abin2", "Abin3", "UNIM1", "UNIM2", "UNIM3"), class = "data.frame", row.names = c(NA, 
-16L))

how can I get the average for each row based on all similar columns 
for example for row A and columns of Abin1,Abin2,Abin3 and UNIM1,UNIM2,UNIM3
the same for all of them 

A:

Here's solution using the dataset you posted, dplyr and tidyr as @Gregor mentioned:
library(dplyr)
library(tidyr)

df %>%
  gather(type, value, -X) %>%                  # reshape dataset
  mutate(type = gsub('[0-9]+', '', type)) %>%  # remove numbers from values (useful for further grouping)
  group_by(X, type) %>%                        # group by columns of interest
  summarise(log_gmean = mean(value)) %>%       # calculate mean
  ungroup() %>%                                # forget the grouping
  spread(type, log_gmean)                      # reshape 

# # A tibble: 16 x 3
#   X          Abin  UNIM
# * <fct>     <dbl> <dbl>
# 1 A           429   726
# 2 AA         1443  1635
# 3 AAA        1531  1911
# 4 AAAA       1231  1209
# 5 AAAAA      1171  1375
# 6 AAAAAA     1661  2070
# 7 AAAAAAA    2725  5049
# 8 B          1627  1865
# 9 BB         1281  1287
# 10 BBB        2762  3627
# 11 BBBB       1679   449
# 12 BBBBB      1556  2153
# 13 BBBBBB     1544  1363
# 14 BBBBBBB     909  1458
# 15 BBBBBBBB   2692  4298
# 16 BBBBBBBBB  9469 11754

Note that the process prints rounded values on your screen because the output is a tibble. Actually, the values have decimal places. You can check that using the viewer, or add %>% data.frame() in the end of your process and you'll see the results as:
#            X      Abin       UNIM
# 1          A  429.0874   725.8419
# 2         AA 1443.4700  1634.5807
# 3        AAA 1531.1868  1911.3928
# 4       AAAA 1230.9150  1208.5479
# 5      AAAAA 1170.7119  1374.7797
# 6     AAAAAA 1661.2184  2069.9936
# 7    AAAAAAA 2724.9919  5048.9097
# 8          B 1626.8301  1865.3136
# 9         BB 1280.6236  1286.7229
# 10       BBB 2761.5521  3627.0985
# 11      BBBB 1678.5880   449.4413
# 12     BBBBB 1555.8990  2153.1276
# 13    BBBBBB 1543.5719  1362.9226
# 14   BBBBBBB  908.5441  1457.5924
# 15  BBBBBBBB 2692.0111  4298.4241
# 16 BBBBBBBBB 9469.2989 11754.0456

