CPTxShamrock
CPTxShamrock

Reputation: 99

trouble getting Lapply with on-the-fly function to run correctly

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?

Upvotes: 2

Views: 153

Answers (2)

Dominic van Essen
Dominic van Essen

Reputation: 872

lapply needs a vector as its argument.
So you could use the indexes of the columns of pros.dat that you want to analyse:

my_columns=c(1,2,3,4) # for instance
lapply(my_columns,function(col) t.test.by.ind(x=pros.dat[,col],y=pros.dat[5]))

Upvotes: 0

StupidWolf
StupidWolf

Reputation: 46888

When you lapply through a data.frame, you are iterating through its columns, so you just apply something similar to what you have in apply, without the margin argument:

pros.dat = data.frame(matrix(rnorm(100*4),ncol=4),ind=rbinom(100,1,0.5))
lapply(pros.dat[,-5],function(i)t.test.by.ind(i,pros.dat[,5]))

Upvotes: 1

Related Questions