Find the row means for columns starting with a string in an R data frame.


To find the row means for columns starting with specific string in an R data frame, we can use mutate function of dplyr package along with rowMeans function.

For Example, if we have a data frame called df that contains three columns say x1_x2, x1_x3, x1_x2 and we want to find the row means for columns x1_x2 and x1_x3 then, we can use the below command −

df%%mutate(X1_Cmbn=select(.,starts_with("x1_")) %% rowMeans())

Example 1

Following snippet creates a sample data frame −

Grp1_x<-rpois(20,2)
Grp1_y<-rpois(20,5)
Grp2_y<-rpois(20,5)
Grp2_x<-rpois(20,2)
df1<-data.frame(Grp1_x,Grp1_y,Grp2_y,Grp2_x)
df1

The following dataframe is created

  Grp1_x Grp1_y Grp2_y Grp2_x
1      1     5       4      4
2      1     3       4      2
3      0     4       7      2
4      2     4       2      1
5      1     3       7      3
6      3     5       4      2
7      4    10       6      3
8      5     5       4      2
9      2     2       5      1
10     7     2       3      4
11     1     3       4      3
12     1     5       4      1
13     2    11       7      6
14     3     8       4      1
15     2     7       5      2
16     2     1       3      1
17     3     3       8      2
18     1     2       4      3
19     1     5       7      2
20     0     4       1      2

To load dplyr package and find the row means of columns having Grp1_ in the column name on the above created data frame, add the following code to the above snippet −

Grp1_x<-rpois(20,2)
Grp1_y<-rpois(20,5)
Grp2_y<-rpois(20,5)
Grp2_x<-rpois(20,2)
df1<-data.frame(Grp1_x,Grp1_y,Grp2_y,Grp2_x)
library(dplyr)
df1%%mutate(Group_1=select(.,starts_with("Grp1_")) %% rowMeans())

Output

If you execute all the above given snippets as a single program, it generates the following Output −

  Grp1_x Grp1_y Grp2_y Grp2_x Group_1
1      1     5      4      4     3.0
2      1     3      4      2     2.0
3      0     4      7      2     2.0
4      2     4      2      1     3.0
5      1     3      7      3     2.0
6      3     5      4      2     4.0
7      4    10      6      3     7.0
8      5     5      4      2     5.0
9      2     2      5      1     2.0
10     7     2      3      4     4.5
11     1     3      4      3     2.0
12     1     5      4      1     3.0
13     2    11      7      6     6.5
14     3     8      4      1     5.5
15     2     7      5      2     4.5
16     2     1      3      1     1.5
17     3     3      8      2     3.0
18     1     2      4      3     1.5
19     1     5      7      2     3.0
20     0     4      1      2     2.0

Example 2

Following snippet creates a sample data frame −

MaleHeight<-sample(150:180,20)
MaleWeight<-sample(50:80,20)
FemaleHeight<-sample(150:180,20)
FemaleWeight<-sample(50:80,20)
df2<-data.frame(MaleHeight,MaleWeight,FemaleHeight,FemaleWeight)
df2

The following dataframe is created

  MaleHeight MaleWeight FemaleHeight FemaleWeight
1       178      61         177           51
2       172      60         169           73
3       174      57         173           53
4       152      62         180           50
5       169      68         168           54
6       177      56         153           68
7       154      69         150           56
8       155      71         175           75
9       160      73         171           60
10      150      50         165           64
11      151      75         156           72
12      168      79         174           78
13      173      64         179           62
14      170      65         178           70
15      161      55         152           65
16      171      67         166           67
17      157      53         158           59
18      153      58         159           55
19      163      72         151           63
20      167      76         170           80

To find the row means of columns having Male in the column name on the above created data frame, add the following code to the above snippet −

MaleHeight<-sample(150:180,20)
MaleWeight<-sample(50:80,20)
FemaleHeight<-sample(150:180,20)
FemaleWeight<-sample(50:80,20)
df2<-data.frame(MaleHeight,MaleWeight,FemaleHeight,FemaleWeight)
df2%%mutate(Males=select(.,starts_with("Male")) %% rowMeans())

Output

If you execute all the above given snippets as a single program, it generates the following Output −

   MaleHeight MaleWeight FemaleHeight FemaleWeight Males
1       178         61         177         51 119.5
2       172         60         169         73 116.0
3       174         57         173         53 115.5
4       152         62         180         50 107.0
5       169         68         168         54 118.5
6       177         56         153         68 116.5
7       154         69         150         56 111.5
8       155         71         175         75 113.0
9       160         73         171         60 116.5
10      150         50         165         64 100.0
11      151         75         156         72 113.0
12      168         79         174         78 123.5
13      173         64         179         62 118.5
14      170         65         178         70 117.5
15      161         55         152         65 108.0
16      171         67         166         67 119.0
17      157         53         158         59 105.0
18      153         58         159         55 105.5
19      163         72         151         63 117.5
20      167         76         170         80 121.5

Updated on: 01-Nov-2021

673 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements