Data:
data <- structure(list(index = c(1, 2, 3, 4, 5, 6),
hour = c(13, 13, 13, 1, 1, 1), minutes = c(31,
31, 32, 36, 36, 36)), class = "data.frame", row.names = c(1067L,
1069L, 1070L, 1072L, 1073L, 1074L))
use do.call:
func <- function(hourFilt, minutesFilt){
filt <- data[data$hour == hourFilt & data$minutes == minutesFilt, ]$idx
sum(filt)
}
do.call(func, list(hourFilt = 1:5, minutesFilt = 31:35))
I am aware what the warning means:
Warning messages: 1: In data$hour == hourFilt : longer object length is not a multiple of shorter object length
but I would expect, that the function is called five times and not only once, resulting in that warning and wrong result.
Expected result:
mapply(func, hourFilt = 1:5, minutesFilt = 31:35)
1 2 0 0 0
However, I would prefer using do.call() as it is faster.
Edit: Adding intention
I want to filter a dataset of 25k-200k rows for a specific time. The filtering on the original data set will result in three rows. And for that result i want to sum up a given variable - here: index. That process will be repeated for a few hundred times. Therefore, i looked into mapply(), do.call(). Performance does matter, thats why i prefer do.call().
mapplylikemapply(func, hourFilt = 1:5, minutesFilt = 31:35)I get all 0's on yourdatawhich makes sense since for the first casedata$index[data$hour == 1 & data$minutes == 31]returnsnumeric(0). How are you getting 1 ? - Ronak Shahdo.callis "faster" as they do completely different things.do.call()will only ever call a function once with one set of parameters. You seem to want to call a function multiple times with different parameters. Those are very different operations. I'm not suredo.calldoes what you think it does. - MrFlick