Why doesn't this work for an example? There's same value in each row and warning as well
data <- data.frame(id = 1:10)
slowCall <- function(id) data.frame(b = rep(id, 3), c = runif(3))
data[,c("d", "e")] <- sapply(data$id, function(id) {
tmp <- slowCall(id)
list(sum(tmp$b), min(tmp$c))
})
Warning message:
In `[<-.data.frame`(`*tmp*`, , c("d", "e"), value = list(3L, 0.104784948984161, :
provided 20 variables to replace 2 variables
print(data)
id d e
1 1 3 0.1047849
2 2 3 0.1047849
3 3 3 0.1047849
4 4 3 0.1047849
5 5 3 0.1047849
6 6 3 0.1047849
7 7 3 0.1047849
8 8 3 0.1047849
9 9 3 0.1047849
10 10 3 0.1047849
You could try something like this. First, vectorize the
assignfunction (per @Joran's answer here), then modify your code slightly.Output:
Note: I invoke
plyr::mlplyto get yoursapplyoutput into a list.The simpler answer, though, is to change the righthand side of your operation into:
which would give you the same result.