I am trying to work with lapply function using a dataframe based on a binary variable in the 5th column.
The function I made is pretty straight forward. a simple t.test and works quite well
t.test.by.ind = function(x, ind) {
stopifnot(all(ind %in% c(0, 1)))
return(t.test(x[ind == 0], x[ind == 1]))
}
now, to the problem, I cannot for the life of me get lapply working. I've tried several variants such as:
##Note that pros.dat = my dataframe
lapply(pros.dat, FUN = function(df){
return(apply(pros.dat[,-5], MARGIN = 2,
FUN = t.test.by.ind, ind = pros.dat[,5]))
})
and
##Note that pros.dat = my dataframe
lapply(pros.dat, FUN = function(df){
return(apply(df[,-5], MARGIN = 2,
FUN = t.test.by.ind, ind = df[,5]))
})
I have the worst time trying to get lapply to work. I can get apply working just fine.
apply(pros.dat[,-5], MARGIN = 2, FUN = t.test.by.ind, ind = pros.dat[,5])
Why can't I see what I'm doing wrong?