0

I have the following data:

dat <- data.frame(ID=1:4,q1=c(2,3,2,3),q2=c(3,2,2,4),q3=c(2,2,3,5),q4=c(4,4,4,6))
ID  q1 q2 q3 q4 
1    2  3  2  4
2    3  2  2  4 
3    2  2  3  4
4    3  4  5  6

I need to have :

  1. the mean of q1 and q3; the mean q2 and q4
  2. the mean of q1 and q2; the mean of q3 and q4

E.g. - for the first one: 2+2/2= 2 (q1+q3) ...etc for each row

thelatemail
  • 91,185
  • 12
  • 128
  • 188
  • @Seth - that is the mean of the sum of the variables, not the mean of the two variables combined. – thelatemail Aug 15 '13 at 01:01
  • It is not clear what the OP is trying to ask here. The mean of all the values of q1 and q3 or the mean of the rowwise sum of q1 and q3. In either case this question has been asked and answered previously. – Seth Aug 15 '13 at 01:17
  • @JasonC - why rollback? - now the question is a) not reproducible b) not clear in the context of R's mean calculations! – thelatemail Aug 15 '13 at 01:45
  • @thelatemail You need to add your clarifications to the comments and leave it to the OP to determine if your interpretation is correct and to make radical changes. I will not have an edit war with you. Please undo changes that add information that the OP did not provide. You've seen the review form, you've seen the reject reasons, you know the drill. Thanks! :) – Jason C Aug 15 '13 at 01:50
  • 2
    @JasonC - the OP clarified their issue in a (now deleted, but visible to 10k+ rep) answer that I copied and pasted from to make the edit. The OP did therefore provide this information, albeit in the wrong location. The edit to provide reproducible data simply replicates the data they already provided in an R input format. It is not changing the meaning of the question. – thelatemail Aug 15 '13 at 01:54

3 Answers3

1

you can use

    mean(c(dat$q1, dat$q2))

Or

    mean(dat[, c("q1", "q2")]

etc

Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178
1

In light of the extra info:

rowMeans(dat[c("q1","q3")])
#[1] 2.0 2.5 2.5 4.0

...etc for whatever combination you need.

thelatemail
  • 91,185
  • 12
  • 128
  • 188
0

Alternatively,

apply(dat[,c("q1","q3")], 1, mean)
apply(dat[,c("q2","q4")], 1, mean)
Remko Duursma
  • 2,741
  • 17
  • 24