r - t.test for all combinations of a grouping factor first by subsetting -


i trying t.test combinations of grouping factors, first choosing , subsetting data based on selection criteria column.

the structure of data: str(mydata)

'data.frame':   240 obs. of  6 variables:  $ group   : chr  "g1" "g1" "g1" "g1" ...  $ category: chr  "cat1" "cat1" "cat1" "cat1" ...  $ subgroup: chr  "sg1" "sg1" "sg1" "sg1" ...  $ score   : num  0.156 0.131 0.092 0.319 0.179 ...  $ sd      : num  0.0768 0.0768 0.0768 0.0768 0.0768 ...  $ se      : num  0.0172 0.0172 0.0172 0.0172 0.0172 ... 

i have 3 groups in the group column: g1, g2 , g3
have 4 categories in category column: cat1, cat2, cat3 , cat4
have twelve subgroups: sg1, sg2, until sg12

currently, creating list of combinations subgroup names first subsetting data based of group id, in example g1 , g3:

combinations <- combn(unique(mydata[mydata$group %in% c("g1", "g3"),]$subgroup),2, simplify = false) 

then doing t.test each of these combinations, referring answer here:

results <- lapply(seq_along(combinations), function (n) {   mydatatemp <- mydata[rownames(mydata$subgroup) %in% unlist(combinations[n]),]   result <- t.test(mydatatemp[,1], mydatatemp[,2], alternative="two.sided", var.equal=true)   return(result)}) results 

the error following:

error in t.test.default(mydatatemp[, 1], mydatatemp[, 2], alternative = "two.sided",  :    not enough 'x' observations in addition: warning message: in mean.default(x) : argument not numeric or logical: returning na 

is there more efficient way this? otherwise, how correct error?

update

actually problem how call values in score column in t.test formula?

your code should this:

results <- lapply(seq_along(combinations), function (n) {   mydatatemp <- mydata[with(mydata, subgroup %in% unlist(combinations2[n]) & group %in% c("g1", "g3")),]   result <- t.test(mydatatemp[                    mydatatemp$subgroup == sapply(combinations[n], "[",1),4],                    mydatatemp[mydatatemp$subgroup == sapply(combinations[n], "[", 2),4],                    alternative="two.sided", var.equal=true)              return(result)}) results 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -