This is probably a very basic question; I have a data-frame with a fake questionnaire with three sets of questions measuring three constructs.

I'm currently reading some research papers which in order to create the construct aggregate the mean per country, the mean per job and the mean per individual.

So in the case below Construct A given by mean(A1+A2+A3) can only be interpreted on country level which means it has to be calculated on country level, Construct B can only be interpreted on career level and Construct C can be interpreted on individual level

Based on the R code below, can someone help me understand how these aggregates are calculated

country <- c("Ireland","England","France","Germany","USA","Spain")
job <- c("IT","SOCIAL","Project Manager","Director","Vice-President")
Q <- (1:7)

set.seed(300)

mydf <- data.frame(countries = sample(country,100,replace = TRUE),
               career = sample(job,100,replace=TRUE),
               participent = (1:100),
               A1 = sample(Q,100,replace=TRUE),
               A2 = sample(Q,100,replace=TRUE),
               A3 = sample(Q,100,replace=TRUE),
               B1 = sample(Q,100,replace=TRUE),
               B2 = sample(Q,100,replace=TRUE),
               B3 = sample(Q,100,replace=TRUE),
               C1 = sample(Q,100,replace=TRUE),
               C2 = sample(Q,100,replace=TRUE),
               C3 = sample(Q,100,replace=TRUE)
               )
up vote 2 down vote accepted
+50

Is this what you are looking for?

> with(mydf, tapply((A1+A2+A3), countries, mean))
England   France  Germany  Ireland    Spain      USA 
12.18182 11.55556 11.47619 11.90909 12.35000 12.89474 
> with(mydf, tapply((B1+B2+B3), career, mean))
   Director              IT Project Manager          SOCIAL  Vice-President 
   11.00000        12.16667        11.78947        10.85000        12.22727 
> mean(mydf$C1+mydf$C2+mydf$C3)
[1] 12.14

  • Hi @Brent , thank you for your quick response. Since the aim of the analysis is hierarchial, could you tell me how you would calculate constructs per career, per country? So IT managers in Spain vs IT managers in Italy – John Smith May 8 '16 at 6:11
  • try this: library(doBy) summaryBy((B1+B2+B3)~countries+career, data=mydf, FUN=mean) – Brent Ferrier May 9 '16 at 12:51

Also look at dplyr. It's really intuitive for these kids on questions. Once you get used to the syntax you will be hooked.

you would do this:

library(dplyr)
mydf %>% group_by(countires,career) %>% summarize( sm=sum(B1+B2+B3 ) , 
 mn=mean(B1+B2+B3), etc... )

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.