I have 3 different groups summarised in a data frame. The data frame looks like:
d <- data.frame(v1 = c("A","A","A","B","B","B","C","C","C"),
v2 = c(1:9), stringsAsFactors = FALSE)
What I want is to compare the values of A against values of B. Also values of A against values of B and as a last comparison the values of B against the values of C
I constructed 2 for loops to iterate over v1 to extract the groups to compare. However, the for-loops give me all possible combinations like:
A vs. A
A vs. B
A vs. C
B vs. A
B vs. B
B vs. C
C vs. A and so on...
Here are my for-loops:
for(i in unique(d$v1)) {
for(j in unique(d$v1)) {
cat("i = ", i, "j = ", j, "\n")
group1 <- d[which(d$v1 == i), ]
group2 <- d[which(d$v1 == j), ]
print(group1)
print(group2)
cat("---------------------\n\n")
}
}
How can I manage to only iterate over data frame d
so that in the first iteration group1 contains the values of A and group2 contains the values of B. In the second iteration group1 contains the values of A and group2 the values of C. And as a last comparisons group1 contains values of B and group2 contains values of C.
I am somehow totally stuck with that problem and hoping to find an answer here.
Cheers!