Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate mean of each numeric column and add as result as row

Tags:

r

So, I would like to calculate the mean of each numeric column and put the results in the row below the column. Let's start with a data:

> head(tbl_mut)

     timetE4_1  timetE1_2  timetE2_2  timetE3_2  timetE4_2   eve_mean   mor_mean  tot_mean
    1   4048.605   59094.48   27675.59   26374.06   43310.01   7774.442   39113.53  23443.99
    2  45729.986  139889.21  111309.64  129781.17   96924.62  43374.117  119476.16  81425.14
    3 639686.154 1764684.16 1117027.29 1147967.45 1156442.48 585562.724 1296530.34 941046.53
    4   4466.153   26250.32   20320.08   18413.54   29061.25   3866.547   23511.30  13688.92

And that's what I want to achieve:

timetE4_1  timetE1_2  timetE2_2  timetE3_2  timetE4_2   eve_mean   mor_mean  tot_mean
    1   4048.605   59094.48   27675.59   26374.06   43310.01   7774.442   39113.53  23443.99
    2  45729.986  139889.21  111309.64  129781.17   96924.62  43374.117  119476.16  81425.14
    3 639686.154 1764684.16 1117027.29 1147967.45 1156442.48 585562.724 1296530.34 941046.53
    4   4466.153   26250.32   20320.08   18413.54   29061.25   3866.547   23511.30  13688.92
    .....
    445    X          X          X          X          X         X           X          X

X - the mean of the values in the column.

Note that the data may contain other, non-numeric columns.

like image 464
Rechlay Avatar asked Dec 19 '22 22:12

Rechlay


1 Answers

Use rbind and colMeans as in:

> rbind(tbl_mut, colMeans = colMeans(tbl_mut))
          timetE4_1  timetE1_2  timetE2_2  timetE3_2  timetE4_2   eve_mean   mor_mean  tot_mean
1          4048.605   59094.48   27675.59   26374.06   43310.01   7774.442   39113.53  23443.99
2         45729.986  139889.21  111309.64  129781.17   96924.62  43374.117  119476.16  81425.14
3        639686.154 1764684.16 1117027.29 1147967.45 1156442.48 585562.724 1296530.34 941046.53
4          4466.153   26250.32   20320.08   18413.54   29061.25   3866.547   23511.30  13688.92
colMeans 173482.724  497479.54  319083.15  330634.05  331434.59 160144.458  369657.83 264901.15

EDIT

Suppose your data frame contains both numeric and non-numeric columns (like the 'Description' column):

> df
  Description  timetE4_1  timetE1_2  timetE2_2  timetE3_2  timetE4_2   eve_mean   mor_mean  tot_mean
1           A   4048.605   59094.48   27675.59   26374.06   43310.01   7774.442   39113.53  23443.99
2           B  45729.986  139889.21  111309.64  129781.17   96924.62  43374.117  119476.16  81425.14
3           C 639686.154 1764684.16 1117027.29 1147967.45 1156442.48 585562.724 1296530.34 941046.53
4           D   4466.153   26250.32   20320.08   18413.54   29061.25   3866.547   23511.30  13688.92

...then you can use sapply(df, is.numeric) to obtain the numeric columns, on which you then calculate colmeans.

> suppressWarnings(rbind(df, colMeans = colMeans(df[, sapply(df, is.numeric)])))
         Description  timetE4_1  timetE1_2  timetE2_2  timetE3_2  timetE4_2   eve_mean   mor_mean  tot_mean
1                  A   4048.605   59094.48   27675.59   26374.06   43310.01   7774.442   39113.53  23443.99
2                  B  45729.986  139889.21  111309.64  129781.17   96924.62  43374.117  119476.16  81425.14
3                  C 639686.154 1764684.16 1117027.29 1147967.45 1156442.48 585562.724 1296530.34 941046.53
4                  D   4466.153   26250.32   20320.08   18413.54   29061.25   3866.547   23511.30  13688.92
colMeans        <NA> 497479.542  319083.15  330634.05  331434.59  160144.46 369657.833  264901.15 173482.72

Or if you know the index of the non-numeric variable, e.g. the first column, you can de-select that column with df[, -1]:

suppressWarnings(rbind(df, colMeans = colMeans(df[, -1]))) 
like image 174
Jilber Urbina Avatar answered Jan 30 '23 21:01

Jilber Urbina