0

I have a matrix

     sex age height weight
[1,]   2   6  110     22
[2,]   1   2   84     12
[3,]   2   2   85     10
[4,]   1   6  108     20
[5,]   2   4  100     18
[6,]   1   4   98     16

How do I calculate mean height for sex = 2 (girls) It should be simple but I can't find the answer.

Ralitza
  • 1
  • 1
  • `mean(data[data$sex==2,3])` – maydin Jan 11 '21 at 14:34
  • Please share your matrix in reproducible format. You can use dput(). – s_baldur Jan 11 '21 at 14:35
  • While you are asking for the mean of just one of the factors, I suspect that you need or *will need* the mean of the other factor as well. That makes this question a duplicate of https://stackoverflow.com/q/11562656/3358272. (Even if you don't eventually want the mean of `sex=1`, you can still take any of those solutions and subset.) – r2evans Jan 11 '21 at 14:41
  • (BTW: that presumes that this should really be a `data.frame` and not a `matrix`. In this case it is not wrong to use a matrix instead, but it is perhaps against the normal flow of data in R that represents *observations* (rows) and *properties* of each observation (columns). If you were to convert your data with `as.data.frame`, any/all of the previously-linked answers should work.) – r2evans Jan 11 '21 at 14:44

0 Answers0