Reputation: 11376
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
Upvotes: 0
Views: 929
Reputation: 7475
To create the possible combinations you can use
combins<-t(combn(levels(myd$mark)[myd$mark],3))
you can then create a function say
dum.fun<-function(x,myd){
dum.match<-match(x,myd$mark)
dum.str<-""
dum.ans<-c()
for(i in 2:6){
dum.str<-paste(myd[dum.match,i],collapse="")
dum.ans[i-1]<-NA
if(dum.str=="ABH"){
dum.ans[i-1]<-0}else{
if(dum.str=="ABB"||dum.str=="AAH"){
dum.ans[i-1]<-1
}}
}
dum.ans
}
then
out<-t(apply(combins,1,dum.fun,myd))
cbind(combins,out)
> head(cbind(combins,out))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] "M1" "M2" "M3" NA "1" NA NA NA
[2,] "M1" "M2" "M4" NA "1" NA NA NA
[3,] "M1" "M2" "M5" NA "0" NA NA NA
[4,] "M1" "M2" "M6" NA "1" NA NA NA
[5,] "M1" "M3" "M4" "0" "1" NA NA NA
[6,] "M1" "M3" "M5" "0" "0" NA NA NA
for example
its all rather messy but hopefully I have grasped what you wanted.
or with one call
t(combn(levels(myd$mark)[myd$mark],3,dum.fun,myd=myd))
Upvotes: 2