The following is example data of my case:
mark <- c(paste("M", 1:6, sep = "")); set.seed(123);
Ind1 <- c(sample (c("A", "B", "H"), 6, replace = T));
set.seed(1234); Ind2 <- c(sample (c("A", "B", "H"), 6, replace = T));
set.seed(12345); Ind3 <- c(sample (c("A", "B", "H"), 6, replace = T));
set.seed (12344);
Ind4 <- c(sample (c("A", "B", "H"), 6, replace = T));
set.seed(1234567); Ind5 <- c(sample (c("A", "B", "H"), 6, replace = T));
myd <- data.frame (mark, Ind1, Ind2, Ind3, Ind4, Ind5)
The data
myd
mark Ind1 Ind2 Ind3 Ind4 Ind5
1 M1 A A H A B
2 M2 H B H H H
3 M3 B B H A H
4 M4 H B H A A
5 M5 H H B A H
6 M6 A B A H B
I want to compare all possible (triplet - 3 at a time) comparison mark for each variables (columns).
M1 & M2 & M3 -> first composition
M1 & M2 & M4 - > second comparison
M1 & M2 & M5
M1 & M2 & M6
M1 & M3 & M4
M1 & M3 & M5
M1 & M3 & M6
M2 & M3 & M4
M2 & M3 & M5
M2 & M3 & M6
......................so on
Thus for the comparison triplet, loop would be: T = Triplet member, T1 = first, T2 = Second, T3 = Third
nevar <- 0
if (T1 =="A", T2 == "B", T3 == "H"){
newvar[i] <- 0
}
else{
if (T1 =="A", T2 == "B", T3 == "B"){
newvar[i] <- 1
} else {
if (T1 =="A", T2 == "A", T3 == "H"){
newvar[i] <- 1
} else {
newvar[i] <- "NA"
}
}}
How can I achieve this ?
Edit:
lets do for Ind1:
first comparison this above list
value of T1 = M1 = "A", T2 = M2 = "H", T3= M3 = "B"
newvar = "NA"
Similarly second comparison:
T1 = M1 = "A", T2 = M2 = "H", T3 = M4 = "H"
newvar = "NA"
M1....M6 rownames (like variable) and I can apply this to all Ind1 ....Ind6, Once look is ready for Ind1