Below is an example of how to randomly pair individuals together who have similar scores based upon two criteria (e.g. reading age and IQ). The first step is to create an artificial dataset. The next step is to turn the criteria, in this case that is iq and level, into z-scores. Transforming the pairing criteria variables into z-scores put them on the same scale. With pairing criteria variables on the same scale, we can combine them creating one criteria that the researchers can pair students with like criteria scores. To transform the criteria, we used the scale function with the center and scale options set to TRUE. We created the single pairing criteria variable by adding the two criteria variables iq and level into one criteria variable iqLevel. Next, we combined the new pairing criteria variable with the id variable allowing us to identify which person received which pairing criteria score. Finally, we ordered the dataset by the largest paring criteria.
set.seed(12345)
readingData = data.frame(id = c(1:10), iq = rnorm(10), level = rnorm(10))
readingData = as.data.frame(readingData)
iq = scale(readingData$iq , center = TRUE, scale = TRUE)
iq = as.data.frame(iq)
level = scale(readingData$level, center = TRUE, scale = TRUE)
iqLevel = as.data.frame(iq+level)
readingData = as.data.frame(cbind(id = readingData$id, iqLevel = iqLevel))
names(readingData) = c("id", "iqLevel")
readingData = as.data.frame(readingData)
readingData = readingData[order(-readingData$iqLevel),]
readingData
## id iqLevel
## 2 2 2.8544479
## 9 9 0.8057538
## 1 1 0.4052056
## 3 3 0.1296129
## 4 4 -0.1157089
## 5 5 -0.3232542
## 7 7 -0.4548486
## 8 8 -0.9096534
## 10 10 -0.9513341
## 6 6 -1.4402210
With the data set ordered by largest pairing criteria score, we can then pair each person with the person directly below them, because the person below the person above them is the person that each person above is closest to. To identify the pairs, we created a parNum or participant number variable to identify if the person was the first or second person in the pair.
Then we grabbed each of the participants by their pairing number and placed them into their own columns. We placed the participants in columns by their pairing numbers, so that we could randomly assign one person in the pair to one condition and the other the opposite. To assign one pair member to one intervention and the other to the opposite, we created a variable called interChoice which contained the numbers one and two. We then randomly sampled from that variable five times (i.e. there are five first pair members in this example) with replacement and assigned each first pair members to the condition that the random sampling produced called interID1. Then we created an ifelse statement that created a variable giving the other pair the opposite condition and called interID2.
readingData$parNum = rep(c(1,2), 5)
parNumOne = readingData[readingData$parNum %in% c(1),]
parNumOne = parNumOne[c(1,3)]
parNumTwo = readingData[readingData$parNum %in% c(2),]
parNumTwo = parNumTwo[c(1,3)]
parNumBoth = cbind(parNumOne, parNumTwo)
names(parNumBoth) = c("id1", "parNum1", "id2", "parNum2")
head(parNumBoth)
## id1 parNum1 id2 parNum2
## 2 2 1 9 2
## 1 1 1 3 2
## 4 4 1 5 2
## 7 7 1 8 2
## 10 10 1 6 2
interChoice = c(1,2)
set.seed(12345)
interID = sample(interChoice, size = 5, replace = TRUE)
parNumBoth$interID1 = interID
parNumBoth$interID2 = ifelse(parNumBoth$interID1 == 1, 2, 1)
parNumBoth
## id1 parNum1 id2 parNum2 interID1 interID2
## 2 2 1 9 2 2 1
## 1 1 1 3 2 2 1
## 4 4 1 5 2 2 1
## 7 7 1 8 2 2 1
## 10 10 1 6 2 1 2
Finally, we cleaned the data set by appending the two pair’s ids and interIDs together creating two variables id and idInter identifying the participant’s id and intervention assignment in order from first id to last.
idTotal = cbind(id1 = parNumBoth$id1,id2 =parNumBoth$id2)
idTotal = as.data.frame(idTotal)
idTotal = append(idTotal$id1, idTotal$id2)
idTotal = as.data.frame(idTotal)
interIDTotal = append(parNumBoth$interID1, parNumBoth$interID2)
interIDTotal = as.data.frame(interIDTotal)
id_interID_Combine = cbind(id = idTotal, interID =interIDTotal)
names(id_interID_Combine) = c("id", "interID")
id_interID_Combine = as.data.frame(id_interID_Combine)
id_interID_Combine = id_interID_Combine[order(id_interID_Combine$id),]
id_interID_Combine
## id interID
## 2 1 2
## 1 2 2
## 7 3 1
## 3 4 2
## 8 5 1
## 10 6 2
## 4 7 2
## 9 8 1
## 6 9 1
## 5 10 1