Arne Stops
Arne Stops

Reputation: 49

Iterative t-tests in dataframe with lapply

I have a dataframe dataset30 with 38 variables. I wanna run t-tests for some of the 37 variables and group by the 38th variable...like this:

t.test (dataset30$Var1[which(dataset30$Var38 == 1)],dataset30$Var[which(dataset30$Var38 == 2)])

I messed around with lapply trying to iterate through all variables as a first step to a solution but failed:

lapply(dataset30, t.test(x[which(dataset30$Var38 == 1)],x[which(dataset30$ == 2)]))

How can it be done?

Upvotes: 1

Views: 588

Answers (1)

PKumar
PKumar

Reputation: 11128

How about this:

Since no example given, I am going to use mtcars as dataset which is present in R environment.

mtcars_subst <- mtcars[,c("mpg", "drat", "hp", "am")] #Sample data
#Final code for running t.test
lapply(mtcars_subst[,c(1,2,3)], function(x)t.test(x ~ am, data=mtcars_subst))
 # iteratively running t.test on column mpg, drat and hp basis am as the category

Updated after clarification:

Logic:

Filtering the cyl values on 4 and 6 , There are in total of three categories in cyl (4,6 and 8) , to select only 4,6. I have used %in% here. Either you filter within lapply or subset the required data outside then use lapply.

mtcars_subst <- mtcars[,c("mpg", "drat", "hp", "cyl")]
lapply(mtcars_subst[mtcars_subst$cyl %in% c(4,6),c(1,2,3)], function(x)t.test(x ~ cyl, data=mtcars_subst[mtcars_subst$cyl %in% c(4,6),])) #comparing certain 2 groups

Upvotes: 2

Related Questions