ddply returns a data frame as output, and assuming that I am reading your question correctly, this is not what you are looking for. I believe that you would like to conduct a series of t-tests using a series of subsets of data, so the only real task is to compile a list of these subsets. After using them, you can use the lapply () function to run a t-test for each subset in your list. I'm sure this is not the most elegant solution, but one way would be to create a list of unique pairs of your colors using the following function:
get.pairs <- function(v){ l <- length(v) n <- sum(1:l-1) a <- vector("list",n) j = 1 k = 2 for(i in 1:n){ a[[i]] <- c(v[j],v[k]) if(k < l){ k <- k + 1 } else { j = j + 1 k = j + 1 } } return(a) }
Now you can use this function to get a list of unique color pairs:
> (color.pairs <- get.pairs(levels(diam$color)))) [[1]] [1] "D" "E" [[2]] [1] "D" "F" ... [[21]] [1] "I" "J"
Now you can use each of these lists to run t.test (or whatever) in your subset of your data frame, for example:
> t.test(price~cut,data=diam[diam$color %in% color.pairs[[1]],]) Welch Two Sample t-test data: price by cut t = 8.1594, df = 427.272, p-value = 3.801e-15 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: 1008.014 1647.768 sample estimates: mean in group Fair mean in group Ideal 3938.711 2610.820
Now use lapply () to run the test for each subset in the list of color pairs:
> lapply(color.pairs,function(x) t.test(price~cut,data=diam[diam$color %in% x,])) [[1]] Welch Two Sample t-test data: price by cut t = 8.1594, df = 427.272, p-value = 3.801e-15 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: 1008.014 1647.768 sample estimates: mean in group Fair mean in group Ideal 3938.711 2610.820 ... [[21]] Welch Two Sample t-test data: price by cut t = 0.8813, df = 375.996, p-value = 0.3787 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -260.0170 682.3882 sample estimates: mean in group Fair mean in group Ideal 4802.912 4591.726
user1327089
source share