0

I have a dataset like this

> df
             a           b            c          d
1   0.15447394 -0.30926550 -0.008613548 -0.7498640
2  -0.19659661 -0.36732210  0.392225112 -0.1035452
3   0.29479910 -0.38156592 -0.329859865 -1.5651672
4  -0.08113478 -1.44865222 -0.812899295  1.5357360
5  -1.09776659  1.17991490 -1.697510447 -0.7703074
6   0.57770785 -1.26403177 -2.212101953 -1.5634655
7  -1.10261224 -0.76119042 -1.025950267  0.8257326
8   1.30880251  0.39814600  0.533489895  0.4268401
9  -0.21925337  0.13862706  0.782090900  0.2611366
10 -0.40633136 -0.01146429 -0.038013060 -0.8353192

I now want to calculate the mean along the rows, however when I use sapply, I get the following result

> sapply(df, mean)
          a           b           c           d 
-0.07679115 -0.28268043 -0.44171425 -0.25382231 

How to force sapply to perform the mean function along the rows?

Anirudh Murali
  • 107
  • 1
  • 10

2 Answers2

0

You can convert to a matrix then use rowMeans

rowMeans(as.matrix(df))
shians
  • 955
  • 1
  • 6
  • 21
  • Didn't check if `rowMeans()` worked on `data.frames`, but I see the method calls `as.matrix()` anyway so I guess it's superfluous. – shians Aug 17 '17 at 07:42
  • I know, but I didn't check the documentation before writing my answer. The first line of `rowMeans()` is `if (is.data.frame(x)) x <- as.matrix(x)` anyway, in general it's just safer to transform into matrix before numerical operations. – shians Aug 17 '17 at 07:49
  • How could it possibly be less efficient than calling directly on the data.frame when the very first line of `rowMeans` calls `as.matrix` anyway? – shians Aug 17 '17 at 07:53
  • I don't see the relation between the first sentence and the second. Purely numerical `data.frames` should be stored as matrices anyway. See [here](https://stackoverflow.com/questions/5158790/should-i-use-a-data-frame-or-a-matrix). – shians Aug 17 '17 at 08:01
  • I think you must have misread something, I have said nothing about converting back to `data.frame`. In the context of this question `rowMean` returns a `numeric` vector that is neither `data.frame` nor `matrix`. – shians Aug 17 '17 at 08:06
  • That's not what "in general" means though. "In general" means in all cases, you're specifically talking about the case where you need to convert back to a data.frame. – shians Aug 17 '17 at 08:35
0

You can also use apply:

apply(df, 1, mean)
loki
  • 9,816
  • 7
  • 56
  • 82