Sorry for the confusing title, this one's a bit hard to describe. Basically, I've got two data tables which look similar to this:
df1 <- data.frame(SNP=c("W", "X", "Y", "Z"),
Gene.ID=c("A", "B", "C", "B"), pval=NA)
df2 <- data.frame(W=c(1, 0, 1), X=c(1, 1, 0), Y=c(0, 0, 1), Z=c(1, 0, 1),
A=c(3.5, 2.5, 3.5), C=c(4.5, 2.5, 1.5), B=c(1.5, 2.5, 1.5))
So all the entries in df1 correspond to column names in df2. My goal is to fill df1$pval with the p-values from a t-test. For every row in df1, I want to do a t-test comparing the df2 column that matches the value of df1$SNP, and compares that against the df2 column that matches the value of df1$Gene.ID.
For example, for the first row in df1, I would want to compare df2$W vs. df2$A, and then return the resulting p-value inside of df1[1, 3]. For the second row, I would compare df2$X vs. df2$B and return the p-value in df1[2, 3]. In other words, something like this:
for (i in 1:nrow(df1)){
test <- t.test(df2[,which(colnames(df2)==df1[i, 1]] ~ df2[,which(colnames(df2)==df1[i, 2]])
df1[i, 3] <- test$p.value
}
But this does not work because you can only select multiple column names using the colnames
function, not just a single column name. Suggestions for how to get around this would be greatly appreciated--or if you have a simpler method in mind, that would be great too.