merge.data.fnc

Copy, Paste and Adapts

combined=merge.data.fnc(males,females, add.subj=T)

combined=merge.data.fnc(dat1,dat2, add.var=T,

pairing.var='ID')

Objetive

It joins two databases, both to add variables or cases.

Add subjects

We assume that we have two databases from different subject and wish to join them in a new one call combined . We will simulate this scenario dividing database OBrienKaiser (men and women) by using subset function.

males=subset(OBrienKaiser, gender=='M')

females=subset(OBrienKaiser, gender=='F')

In order to add subjects, both databases must have the same number and names of variables.

combined=merge.data.fnc(males,females, add.subj=T)

The function tells you the dimensions (rows and columns) of databases to be joined by adding more subjects ( add.sub = T ). You can see that we start with 8 subjects in each one and end with 16 when combined.

Add variables

On many occasions we have two databases with the same subject but different variables. We will start from a situation where on one side we have a database named MathAchieve with records of 7185 children in 6 variables and other database MathAchSchool 6 variables belonging to 160 schools where children of first daba belong to (Databases belonging to the library nlme ).

Thus we activate both databases.

data(package='nlme','MathAchieve')

data(package='nlme','MathAchSchool')

We look at the first 6 records from both databases.

head(MathAchieve)

head( MathAchSchool)

In the first we see that the first 6 records belong to 6 children belonging to the same school (1224). In the second one you can see that each record belongs to a different school (1224 to 1358). We also see that both bases have in common the variable School , which will use as the pairing variable.

Because we want to add to the base of the characteristics of schools stored in the variables of the second first data. With add.var =T argument, we will join the two databases.

students=merge.data.fnc(MathAchieve,MathAchSchool,

add.var=T, pairing.var='School')

We got an error because the variable MEANSES is in both databases. To fix it, "remove" this variable (column 6) of one of them.

students=merge.data.fnc(MathAchieve[,-6],MathAchSchool,

add.var=T, pairing.var='School')

The function tells you that your registration unit (variable pairing) School occupies more than one row. Note that the first data matrix is a 7185 x 5 and the second 160 x 7. The combined matrix has the same number of rows as the first and the number of columns is just the sum of the number of variables of both.

head(students)