Adding Margins to a Dataframe

Suppose we have a dataframe showing sales by season and by person:

frame<-read.table("c:\\temp\\sales.txt",header=T)
frame

           name  spring  summer  autumn  winter
1    Jane.Smith      14      18      11      12
2  Robert.Jones      17      18      10      13
3      Dick.Rogers  12  16   9  14
4  William.Edwards  15  14  11  10
5      Janet.Jones  11  17  11  16

and we want to add margins to this dataframe showing departures of the seasonal means from the overall mean (as an extra row at the bottom) and departures of the peoples’ means (as an extra column on the right). Finally, we want the sales in the body of the dataframe to be represented by departures from the overall mean.

people<-rowMeans(frame[,2:5])
people<-people-mean(people)
people

      1     2        3        4       5
   0.30  1.05    -0.70    -0.95    0.30

It is very straightforward to add a new column to the dataframe using cbind:

(new.frame<-cbind(frame,people))

              name  spring  summer  autumn  winter  people
1       Jane.Smith      14      18      11      12    0.30
2     Robert.Jones      17      18      10      13    1.05
3      Dick.Rogers      12      16       9      14   -0.70
4  William.Edwards      15      14      11      10   -0.95
5      Janet.Jones      11      17      11      16    0.30

Robert Jones is the most effective sales person (+1.05) and William Edwards is the least effective (−0.95). The column means are calculated in a similar way:

seasons<-colMeans(frame[,2:5])
seasons<-seasons-mean(seasons)
seasons

spring  summer  autumn  winter
  0.35    3.15   -3.05   -0.45

Sales are highest in summer (+3.15) and lowest in autumn (−3.05).

Now there is a hitch, however, because there are only four column means but there are six columns ...

Get The R Book now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.