mapply

R data.table apply function to rows using columns as arguments

左心房为你撑大大i 提交于 2019-11-27 07:16:29
I have the following data.table x = structure(list(f1 = 1:3, f2 = 3:5), .Names = c("f1", "f2"), row.names = c(NA, -3L), class = c("data.table", "data.frame")) I would like to apply a function to each row of the data.table . The function func.test uses args f1 and f2 and does something with it and returns a computed value. Assume (as an example) func.text <- function(arg1,arg2){ return(arg1 + exp(arg2))} but my real function is more complex and does loops and all, but returns a computed value. What would be the best way to accomplish this? The best way is to write a vectorized function, but if

Force mapply to return a list?

China☆狼群 提交于 2019-11-26 17:47:09
问题 Suppose I have a function that creates data frames. I'd like to run that function with different input values, and then rbind the results together into one big data frame, as below: CreateDataFrame <- function(type="A", n=10, n.true=8) { data.frame(success=c(rep(TRUE, n.true), rep(FALSE, n - n.true)), type=type) } df <- do.call(rbind, lapply(toupper(letters[1:5]), CreateDataFrame)) My CreateDataFrame function takes three arguments. In the example above, the second and third arguments are held

R data.table apply function to rows using columns as arguments

旧巷老猫 提交于 2019-11-26 13:08:59
问题 I have the following data.table x = structure(list(f1 = 1:3, f2 = 3:5), .Names = c(\"f1\", \"f2\"), row.names = c(NA, -3L), class = c(\"data.table\", \"data.frame\")) I would like to apply a function to each row of the data.table . The function func.test uses args f1 and f2 and does something with it and returns a computed value. Assume (as an example) func.text <- function(arg1,arg2){ return(arg1 + exp(arg2))} but my real function is more complex and does loops and all, but returns a

Adaptive moving average - top performance in R

拥有回忆 提交于 2019-11-26 05:24:48
问题 I am looking for some performance gains in terms of rolling/sliding window functions in R. It is quite common task which can be used in any ordered observations data set. I would like to share some of my findings, maybe somebody would be able to provide feedback to make it even faster. Important note is that I focus on the case align=\"right\" and adaptive rolling window, so width is a vector (same length as our observation vector). In case if we have width as scalar there are already very