Monday, 19 August 2013

Is it possible use column name instead of number as in a function passed to apply?

Is it possible use column name instead of number as in a function passed
to apply?

I would like to apply a function to each row of a data.frame/data.table
cases <- expand.grid(a=c(TRUE,FALSE), b=c(TRUE,FALSE), c=c(TRUE,FALSE))
myFun <- function(data, row){
otherFun(data, row[1], row[2], row[3])
}
apply(cases, 1, myFun, data=dt1)
This works, however,
myFun <- function(data, row){
otherFun(data, row$a, row$b, row$c)
}
This doesn't work.
What's the best way to do it so that it doesn't have to depend on column
orders?

No comments:

Post a Comment