I think I'm getting a scoping error when using transformBy(), part of the doBy package for R. Here is a simple example of the problem:
> library(doBy)
>
> test.data = data.frame(
+ herp = c(1,2,3,4,5),
+ derp = c(2,3,1,3,5)
+ )
>
> transformData = function(data){
+
+ five = 5
+
+ transformBy(
+ ~ herp,
+ data=data,
+ sum=herp + derp + five
+ )
+ }
>
> transformData(test.data)
Error in eval(expr, envir, enclos) : object 'five' not found
When I run transformBy() within a sub-scope (non-global scope) no local variables or functions seem to be available for use in transformBy. If, on the other hand, I define those variables or functions globally, they become available. Here is a slightly modified example that works:
> library(doBy)
>
> test.data = data.frame(
+ herp = c(1,2,3,4,5),
+ derp = c(2,3,1,3,5)
+ )
>
> five = 5
>
> transformData = function(data){
+ transformBy(
+ ~ herp,
+ data=data,
+ sum=herp + derp + five
+ )
+ }
>
> transformData(test.data)
herp derp sum
1 1 2 8
2 2 3 10
3 3 1 9
4 4 3 12
5 5 5 15
Am I misunderstanding something about how transformBy is supposed to work or is something broken?
Versions:
- ubuntu: 8.04 (x64)
- R: 2.10.1
- doBy: 4.0.5