I want to analyze data from TIMSS on mathematics. Data contains two data sets one for student level information (i.e. level-1) and another one for school level information (i.e. level-2). Data sets are provided in two different files and each file contains school ID variable.

As far as I know, I can't do multilevel analysis with these two data sets and these have to be combined into one data set.Am I right?

I tried to use reshape and reshape2 for combining the two data set but it didn't work. In level-1 data I have SchoolID,motivation, confidence, learningApproach and math scores. Data looks like:

SchoolID studentID motivation confidence learningApproach math
100123  10012301    23  10  12        45
100123  10012302    32  5   16        67
100123  10012303    14  7   21        89
110223  11023301    13  7   18        77
110223  11023302    24  7   11        98
110223  11023303    16  8   12        56

In level-2 data I have SchoolID and resources. Data looks like:

SchoolID    resources
100123      45.5
100223      66.7
1106303     34
110223      22.3

Could you please guide me on how to combine these two data sets in an appropriate way for using with lme4 package?



1 Answer 1


Try using merge():

combined <- merge(x=level1data, y=level2data, by="SchoolID")
  • $\begingroup$ this is what I was looking for.Is there any reverse method? $\endgroup$
    – Amin
    Dec 13, 2013 at 6:10
  • $\begingroup$ @Amin what do you mean by reverse? do you mean getting back to your level-2 data? I think what I would do would be this: level2data <- combined[duplicated(combined$SchoolID) == FALSE, c("SchoolID", "resources")]. The bit duplicated(combined$SchoolID) == FALSE selects rows for the first occurrence of each SchoolID, then c("SchoolID", "resources") only keep the SchoolID and resources columns. $\endgroup$ Dec 14, 2013 at 0:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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