Split and combine data frames - dplyr

Install required packages

dplyr

Simulate data

   id sex group age  IQ rating
1   1   f     T  25  95      5
2   2   f     T  24  84      5
3   3   f    CG  27  99      3
4   4   m    WL  26 116      5
5   5   f     T  21  98      4
6   6   m    WL  31  83      4
7   7   m    CG  34  88      0
8   8   m    CG  28 110      3
9   9   f     T  24  95      1
10 10   f    WL  29  80      2
11 11   m    CG  32  91      4
12 12   m    WL  27  98      2
  id sex age rating
1 21   f  48      3

Combine data frames

Bind rows - does not require identical order of variables / identical variables

   id sex group age IQ rating
11 11   m    CG  32 91      4
12 12   m    WL  27 98      2
13 21   f  <NA>  48 NA      3

Merge data frames

Very good illustration of different join types

Left outer join

  ID  DV
1  1  82
2  1 107
3  2 112
4  2  98
5  3 119
6  3  86
  ID IV sex
1  1  A   f
2  2  B   f
3  3  A   m
  ID  DV IV sex
1  1  82  A   f
2  1 107  A   f
3  2 112  B   f
4  2  98  B   f
5  3 119  A   m
6  3  86  A   m

Specify identical variables which have different names in the two data sets.

  ID initials IV1 DV1
1  1       AB   -  10
2  2       CD   -  10
3  3       EF   +  11
4  4       GH   +  14
  ID_mod initials IV2 DV2
1      3       EF   A  92
2      4       GH   B  79
3      5       IJ   A 101
4      6       KL   B  81
  ID initials IV1 DV1  IV2 DV2
1  1       AB   -  10 <NA>  NA
2  2       CD   -  10 <NA>  NA
3  3       EF   +  11    A  92
4  4       GH   +  14    B  79

Right outer join

  ID initials  IV1 DV1 IV2 DV2
1  3       EF    +  11   A  92
2  4       GH    +  14   B  79
3  5       IJ <NA>  NA   A 101
4  6       KL <NA>  NA   B  81

Full outer join

  ID initials  IV1 DV1  IV2 DV2
1  1       AB    -  10 <NA>  NA
2  2       CD    -  10 <NA>  NA
3  3       EF    +  11    A  92
4  4       GH    +  14    B  79
5  5       IJ <NA>  NA    A 101
6  6       KL <NA>  NA    B  81

Inner join

  ID initials IV1 DV1 IV2 DV2
1  3       EF   +  11   A  92
2  4       GH   +  14   B  79

Detach (automatically) loaded packages (if possible)

Get the article source from GitHub

R markdown - markdown - R code - all posts