wants <- c("dplyr")
has <- wants %in% rownames(installed.packages())
if(any(!has)) install.packages(wants[!has])
set.seed(123)
N <- 12
sex <- factor(sample(c("f", "m"), N, replace=TRUE), levels=c("f", "m"))
group <- factor(sample(rep(c("CG", "WL", "T"), 4), N, replace=FALSE), levels=c("CG", "WL", "T"))
age <- sample(18:35, N, replace=TRUE)
IQ <- round(rnorm(N, mean=100, sd=15))
rating <- round(runif(N, min=0, max=6))
(myDf1 <- data.frame(id=1:N, sex, group, age, IQ, rating))
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
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
Very good illustration of different join types
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.
(dfA <- data.frame(ID=1:4,
initials=c("AB", "CD", "EF", "GH"),
IV1=c("-", "-", "+", "+"),
DV1=c(10, 10, 11, 14),
stringsAsFactors=FALSE))
ID initials IV1 DV1
1 1 AB - 10
2 2 CD - 10
3 3 EF + 11
4 4 GH + 14
(dfB <- data.frame(ID_mod=3:6,
initials=c("EF", "GH", "IJ", "KL"),
IV2=c("A", "B", "A", "B"),
DV2=c(92, 79, 101, 81),
stringsAsFactors=FALSE))
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
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
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
ID initials IV1 DV1 IV2 DV2
1 3 EF + 11 A 92
2 4 GH + 14 B 79
R markdown - markdown - R code - all posts