Consider this example:
data = data.frame(
id = c(1, 2, 3, 4, 5),
value = runif(5)
)
bar <- function(N) {
if (length(N) > 1)
stop("N too long")
return(N)
}
data = transform(data, foo = bar(value))
My code errors out at stop("N too long")
. I expected only one value at a time.
Why does R pass a complete column to N
rather than just one value at a time? To me, this is very counterintuitive.
What can I do instead of this, when I want a new column based on a function return, considering that this function may not only have one argument, but more than one? The function does not work with vectors—it needs to be run one row at a time.
Certainly, the solution can't be to do:
data = ddply(data, .(id), function(row) {
return(transform(foo = bar(row$value)))
})