Can anyone please tell me why I am getting this error?
library(zoo)
Z.index <- as.Date(sample(12450:15500, 3000))
Z.data <- matrix(rnorm(300), ncol = 1)
data1 <- zoo(Z.data, Z.index)
fnc = function(data1)
{
selection2 = select.list(c("Mean"), multiple = F)
Mean = function(dataa) mean(dataa)
return(aggregate(data1, as.yearqtr, selection2))
}
fnc(data1)
I got following error :
Error in get(as.character(FUN), mode = "function", envir = envir) :
variable "Mean" of mode "function" was not found
What would be the correct way?
Regards,
On Sun, Jul 6, 2008 at 3:19 PM, Megh Dal <megh700004 at yahoo.com> wrote:> Can anyone please tell me why I am getting this error? > > library(zoo) > Z.index <- as.Date(sample(12450:15500, 3000)) > Z.data <- matrix(rnorm(300), ncol = 1) > > data1 <- zoo(Z.data, Z.index) > > fnc = function(data1) > { > selection2 = select.list(c("Mean"), multiple = F) > > Mean = function(dataa) mean(dataa) > > return(aggregate(data1, as.yearqtr, selection2)) > } > > fnc(data1) > > I got following error : > Error in get(as.character(FUN), mode = "function", envir = envir) : > variable "Mean" of mode "function" was not found >Its a bug in aggregate.zoo . Its just been fixed in the zoo devel version available on R-Forge so you can either grab that or use the workaround below: library(zoo) set.seed(1) Z.data <- matrix(rnorm(300), ncol = 1) Z.index <- as.Date(sample(12450:15500, 3000)) data1 <- zoo(Z.data, Z.index) fnc <- function(data1) { selection2 <- select.list("Mean", multiple = FALSE) Mean <- function(dataa) mean(dataa) my.match.fun <- function(x) match.fun(x) ### selection2 <- my.match.fun(selection2) ### return(aggregate(data1, as.yearqtr, selection2)) } fnc(data1)
I made some changes and also incorporated your advice :
library(zoo)
Z.index <- as.Date(sample(12450:15500, 3000))
Z.data <- matrix(rnorm(300), ncol = 1)
data1 <- zoo(Z.data, Z.index)
fnc = function(data1)
{
selection2 = select.list(c("Mean", "SD"), multiple = T)
Mean = function(dataa) mean(dataa)
my.match.fun <- function(x) match.fun(x) ###
selection2 <- my.match.fun(selection2) ###
ag = function(z, by, selection2)
{
f = function(f) aggregate(z, by, f)
do.call(cbind, sapply(selection2, f, simplify = F))
}
return(aggregate(data1, as.yearqtr, selection2))
}
fnc(data1)
But still getting same error.
--- On Mon, 7/7/08, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> From: Gabor Grothendieck <ggrothendieck at gmail.com>
> Subject: Re: [R] Error in defining function
> To: megh700004 at yahoo.com
> Cc: r-help at stat.math.ethz.ch
> Date: Monday, July 7, 2008, 1:23 AM
> On Sun, Jul 6, 2008 at 3:19 PM, Megh Dal
> <megh700004 at yahoo.com> wrote:
> > Can anyone please tell me why I am getting this error?
> >
> > library(zoo)
> > Z.index <- as.Date(sample(12450:15500, 3000))
> > Z.data <- matrix(rnorm(300), ncol = 1)
> >
> > data1 <- zoo(Z.data, Z.index)
> >
> > fnc = function(data1)
> > {
> > selection2 = select.list(c("Mean"),
> multiple = F)
> >
> > Mean = function(dataa) mean(dataa)
> >
> > return(aggregate(data1, as.yearqtr, selection2))
> > }
> >
> > fnc(data1)
> >
> > I got following error :
> > Error in get(as.character(FUN), mode > "function", envir
= envir) :
> > variable "Mean" of mode
> "function" was not found
> >
>
> Its a bug in aggregate.zoo . Its just been fixed in the
> zoo devel version
> available on R-Forge so you can either grab that or use the
> workaround
> below:
>
> library(zoo)
>
> set.seed(1)
> Z.data <- matrix(rnorm(300), ncol = 1)
> Z.index <- as.Date(sample(12450:15500, 3000))
> data1 <- zoo(Z.data, Z.index)
>
> fnc <- function(data1) {
> selection2 <- select.list("Mean", multiple
> = FALSE)
> Mean <- function(dataa) mean(dataa)
>
> my.match.fun <- function(x) match.fun(x) ###
> selection2 <- my.match.fun(selection2) ###
>
> return(aggregate(data1, as.yearqtr, selection2))
> }
>
> fnc(data1)
Your program works as is if you choose Mean but you have
introduced two new errors:
1. SD is not defined in your program.
2. if multiple choices are taken then it will try to pass a vector to
my.match.fun but that calls match.fun which only allows functions
to be passed to it. You will have to translate each character string
to a function separately.
Try this:
funs <- c("Mean", "SD")
f <- function(fun) aggregate(data1, as.yearqtr, get(fun))
do.call(cbind, sapply(tolower(funs), f, simplify = FALSE))
On Sun, Jul 6, 2008 at 10:43 PM, Megh Dal <megh700004 at yahoo.com>
wrote:> I made some changes and also incorporated your advice :
>
> library(zoo)
> Z.index <- as.Date(sample(12450:15500, 3000))
> Z.data <- matrix(rnorm(300), ncol = 1)
>
> data1 <- zoo(Z.data, Z.index)
>
> fnc = function(data1)
> {
> selection2 = select.list(c("Mean", "SD"), multiple =
T)
>
> Mean = function(dataa) mean(dataa)
> my.match.fun <- function(x) match.fun(x) ###
> selection2 <- my.match.fun(selection2) ###
> ag = function(z, by, selection2)
> {
> f = function(f) aggregate(z, by, f)
> do.call(cbind, sapply(selection2, f, simplify = F))
> }
>
> return(aggregate(data1, as.yearqtr, selection2))
> }
>
> fnc(data1)
>
> But still getting same error.
>
>
> --- On Mon, 7/7/08, Gabor Grothendieck <ggrothendieck at gmail.com>
wrote:
>
>> From: Gabor Grothendieck <ggrothendieck at gmail.com>
>> Subject: Re: [R] Error in defining function
>> To: megh700004 at yahoo.com
>> Cc: r-help at stat.math.ethz.ch
>> Date: Monday, July 7, 2008, 1:23 AM
>> On Sun, Jul 6, 2008 at 3:19 PM, Megh Dal
>> <megh700004 at yahoo.com> wrote:
>> > Can anyone please tell me why I am getting this error?
>> >
>> > library(zoo)
>> > Z.index <- as.Date(sample(12450:15500, 3000))
>> > Z.data <- matrix(rnorm(300), ncol = 1)
>> >
>> > data1 <- zoo(Z.data, Z.index)
>> >
>> > fnc = function(data1)
>> > {
>> > selection2 = select.list(c("Mean"),
>> multiple = F)
>> >
>> > Mean = function(dataa) mean(dataa)
>> >
>> > return(aggregate(data1, as.yearqtr, selection2))
>> > }
>> >
>> > fnc(data1)
>> >
>> > I got following error :
>> > Error in get(as.character(FUN), mode >>
"function", envir = envir) :
>> > variable "Mean" of mode
>> "function" was not found
>> >
>>
>> Its a bug in aggregate.zoo . Its just been fixed in the
>> zoo devel version
>> available on R-Forge so you can either grab that or use the
>> workaround
>> below:
>>
>> library(zoo)
>>
>> set.seed(1)
>> Z.data <- matrix(rnorm(300), ncol = 1)
>> Z.index <- as.Date(sample(12450:15500, 3000))
>> data1 <- zoo(Z.data, Z.index)
>>
>> fnc <- function(data1) {
>> selection2 <- select.list("Mean", multiple
>> = FALSE)
>> Mean <- function(dataa) mean(dataa)
>>
>> my.match.fun <- function(x) match.fun(x) ###
>> selection2 <- my.match.fun(selection2) ###
>>
>> return(aggregate(data1, as.yearqtr, selection2))
>> }
>>
>> fnc(data1)
>
>
>
>
"You will have to translate each character string
to a function separately."
Here I have a modified program :
library(zoo)
Z.index <- as.Date(sample(12450:15500, 3000))
Z.data <- matrix(rnorm(300), ncol = 1)
data1 <- zoo(Z.data, Z.index)
fnc = function(data1)
{
selection2 = select.list(c("Mean", "SD"), multiple = T)
Mean = function(dataa) mean(dataa)
SD = function(dataa) sd(dataa)
my.match.fun <- function(x) match.fun(x) ###
for (i in length(selection2))
{
selection2[i] <- my.match.fun(selection2[i]) ###
}
ag = function(z, by, selection2)
{
f = function(f) aggregate(z, by, f)
do.call(cbind, sapply(selection2, f, simplify = F))
}
return(ag(data1, as.yearqtr, selection2))
}
fnc(data1)
Frustrated !! still error. I also have tried downloading from R-forge. But
download page is not working. I then tried directly from R using
install.packages. Still there is no improvement.
Regards,
--- On Mon, 7/7/08, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> From: Gabor Grothendieck <ggrothendieck at gmail.com>
> Subject: Re: [R] Error in defining function
> To: megh700004 at yahoo.com
> Cc: r-help at stat.math.ethz.ch
> Date: Monday, July 7, 2008, 8:44 AM
> Your program works as is if you choose Mean but you have
> introduced two new errors:
>
> 1. SD is not defined in your program.
>
> 2. if multiple choices are taken then it will try to pass a
> vector to
> my.match.fun but that calls match.fun which only allows
> functions
> to be passed to it. You will have to translate each
> character string
> to a function separately.
>
> Try this:
>
> funs <- c("Mean", "SD")
> f <- function(fun) aggregate(data1, as.yearqtr,
> get(fun))
> do.call(cbind, sapply(tolower(funs), f, simplify = FALSE))
>
>
> On Sun, Jul 6, 2008 at 10:43 PM, Megh Dal
> <megh700004 at yahoo.com> wrote:
> > I made some changes and also incorporated your advice
> :
> >
> > library(zoo)
> > Z.index <- as.Date(sample(12450:15500, 3000))
> > Z.data <- matrix(rnorm(300), ncol = 1)
> >
> > data1 <- zoo(Z.data, Z.index)
> >
> > fnc = function(data1)
> > {
> > selection2 = select.list(c("Mean",
> "SD"), multiple = T)
> >
> > Mean = function(dataa) mean(dataa)
> > my.match.fun <- function(x) match.fun(x) ###
> > selection2 <- my.match.fun(selection2) ###
> > ag = function(z, by, selection2)
> > {
> > f = function(f) aggregate(z,
> by, f)
> > do.call(cbind,
> sapply(selection2, f, simplify = F))
> > }
> >
> > return(aggregate(data1, as.yearqtr, selection2))
> > }
> >
> > fnc(data1)
> >
> > But still getting same error.
> >
> >
> > --- On Mon, 7/7/08, Gabor Grothendieck
> <ggrothendieck at gmail.com> wrote:
> >
> >> From: Gabor Grothendieck
> <ggrothendieck at gmail.com>
> >> Subject: Re: [R] Error in defining function
> >> To: megh700004 at yahoo.com
> >> Cc: r-help at stat.math.ethz.ch
> >> Date: Monday, July 7, 2008, 1:23 AM
> >> On Sun, Jul 6, 2008 at 3:19 PM, Megh Dal
> >> <megh700004 at yahoo.com> wrote:
> >> > Can anyone please tell me why I am getting
> this error?
> >> >
> >> > library(zoo)
> >> > Z.index <- as.Date(sample(12450:15500,
> 3000))
> >> > Z.data <- matrix(rnorm(300), ncol = 1)
> >> >
> >> > data1 <- zoo(Z.data, Z.index)
> >> >
> >> > fnc = function(data1)
> >> > {
> >> > selection2 > select.list(c("Mean"),
> >> multiple = F)
> >> >
> >> > Mean = function(dataa) mean(dataa)
> >> >
> >> > return(aggregate(data1, as.yearqtr,
> selection2))
> >> > }
> >> >
> >> > fnc(data1)
> >> >
> >> > I got following error :
> >> > Error in get(as.character(FUN), mode > >>
"function", envir = envir) :
> >> > variable "Mean" of mode
> >> "function" was not found
> >> >
> >>
> >> Its a bug in aggregate.zoo . Its just been fixed
> in the
> >> zoo devel version
> >> available on R-Forge so you can either grab that
> or use the
> >> workaround
> >> below:
> >>
> >> library(zoo)
> >>
> >> set.seed(1)
> >> Z.data <- matrix(rnorm(300), ncol = 1)
> >> Z.index <- as.Date(sample(12450:15500, 3000))
> >> data1 <- zoo(Z.data, Z.index)
> >>
> >> fnc <- function(data1) {
> >> selection2 <- select.list("Mean",
> multiple
> >> = FALSE)
> >> Mean <- function(dataa) mean(dataa)
> >>
> >> my.match.fun <- function(x) match.fun(x)
> ###
> >> selection2 <- my.match.fun(selection2) ###
> >>
> >> return(aggregate(data1, as.yearqtr,
> selection2))
> >> }
> >>
> >> fnc(data1)
> >
> >
> >
> >
The new code you posted has introduced two more bugs yet
again.
Before posting again run your code in the debugger issuing this
command prior to testing fnc:
debug(fnc)
and then single step through the code. That way you can find
your own bugs. See ?debug
You can source aggregate.zoo from R-Forge via:
source("http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/*checkout*/pkg/R/aggregate.zoo.R?rev=483&root=zoo")
On Mon, Jul 7, 2008 at 3:19 AM, Megh Dal <megh700004 at yahoo.com>
wrote:> "You will have to translate each character string
> to a function separately."
>
> Here I have a modified program :
>
> library(zoo)
> Z.index <- as.Date(sample(12450:15500, 3000))
> Z.data <- matrix(rnorm(300), ncol = 1)
>
> data1 <- zoo(Z.data, Z.index)
>
> fnc = function(data1)
> {
> selection2 = select.list(c("Mean", "SD"), multiple =
T)
>
> Mean = function(dataa) mean(dataa)
> SD = function(dataa) sd(dataa)
>
> my.match.fun <- function(x) match.fun(x) ###
>
> for (i in length(selection2))
> {
> selection2[i] <- my.match.fun(selection2[i]) ###
> }
>
> ag = function(z, by, selection2)
> {
> f = function(f) aggregate(z, by, f)
> do.call(cbind, sapply(selection2, f, simplify = F))
> }
>
> return(ag(data1, as.yearqtr, selection2))
> }
>
> fnc(data1)
>
> Frustrated !! still error. I also have tried downloading from R-forge. But
download page is not working. I then tried directly from R using
install.packages. Still there is no improvement.
>
> Regards,
>
>
> --- On Mon, 7/7/08, Gabor Grothendieck <ggrothendieck at gmail.com>
wrote:
>
>> From: Gabor Grothendieck <ggrothendieck at gmail.com>
>> Subject: Re: [R] Error in defining function
>> To: megh700004 at yahoo.com
>> Cc: r-help at stat.math.ethz.ch
>> Date: Monday, July 7, 2008, 8:44 AM
>> Your program works as is if you choose Mean but you have
>> introduced two new errors:
>>
>> 1. SD is not defined in your program.
>>
>> 2. if multiple choices are taken then it will try to pass a
>> vector to
>> my.match.fun but that calls match.fun which only allows
>> functions
>> to be passed to it. You will have to translate each
>> character string
>> to a function separately.
>>
>> Try this:
>>
>> funs <- c("Mean", "SD")
>> f <- function(fun) aggregate(data1, as.yearqtr,
>> get(fun))
>> do.call(cbind, sapply(tolower(funs), f, simplify = FALSE))
>>
>>
>> On Sun, Jul 6, 2008 at 10:43 PM, Megh Dal
>> <megh700004 at yahoo.com> wrote:
>> > I made some changes and also incorporated your advice
>> :
>> >
>> > library(zoo)
>> > Z.index <- as.Date(sample(12450:15500, 3000))
>> > Z.data <- matrix(rnorm(300), ncol = 1)
>> >
>> > data1 <- zoo(Z.data, Z.index)
>> >
>> > fnc = function(data1)
>> > {
>> > selection2 = select.list(c("Mean",
>> "SD"), multiple = T)
>> >
>> > Mean = function(dataa) mean(dataa)
>> > my.match.fun <- function(x) match.fun(x) ###
>> > selection2 <- my.match.fun(selection2) ###
>> > ag = function(z, by, selection2)
>> > {
>> > f = function(f) aggregate(z,
>> by, f)
>> > do.call(cbind,
>> sapply(selection2, f, simplify = F))
>> > }
>> >
>> > return(aggregate(data1, as.yearqtr, selection2))
>> > }
>> >
>> > fnc(data1)
>> >
>> > But still getting same error.
>> >
>> >
>> > --- On Mon, 7/7/08, Gabor Grothendieck
>> <ggrothendieck at gmail.com> wrote:
>> >
>> >> From: Gabor Grothendieck
>> <ggrothendieck at gmail.com>
>> >> Subject: Re: [R] Error in defining function
>> >> To: megh700004 at yahoo.com
>> >> Cc: r-help at stat.math.ethz.ch
>> >> Date: Monday, July 7, 2008, 1:23 AM
>> >> On Sun, Jul 6, 2008 at 3:19 PM, Megh Dal
>> >> <megh700004 at yahoo.com> wrote:
>> >> > Can anyone please tell me why I am getting
>> this error?
>> >> >
>> >> > library(zoo)
>> >> > Z.index <- as.Date(sample(12450:15500,
>> 3000))
>> >> > Z.data <- matrix(rnorm(300), ncol = 1)
>> >> >
>> >> > data1 <- zoo(Z.data, Z.index)
>> >> >
>> >> > fnc = function(data1)
>> >> > {
>> >> > selection2 >> select.list(c("Mean"),
>> >> multiple = F)
>> >> >
>> >> > Mean = function(dataa) mean(dataa)
>> >> >
>> >> > return(aggregate(data1, as.yearqtr,
>> selection2))
>> >> > }
>> >> >
>> >> > fnc(data1)
>> >> >
>> >> > I got following error :
>> >> > Error in get(as.character(FUN), mode >> >>
"function", envir = envir) :
>> >> > variable "Mean" of mode
>> >> "function" was not found
>> >> >
>> >>
>> >> Its a bug in aggregate.zoo . Its just been fixed
>> in the
>> >> zoo devel version
>> >> available on R-Forge so you can either grab that
>> or use the
>> >> workaround
>> >> below:
>> >>
>> >> library(zoo)
>> >>
>> >> set.seed(1)
>> >> Z.data <- matrix(rnorm(300), ncol = 1)
>> >> Z.index <- as.Date(sample(12450:15500, 3000))
>> >> data1 <- zoo(Z.data, Z.index)
>> >>
>> >> fnc <- function(data1) {
>> >> selection2 <- select.list("Mean",
>> multiple
>> >> = FALSE)
>> >> Mean <- function(dataa) mean(dataa)
>> >>
>> >> my.match.fun <- function(x) match.fun(x)
>> ###
>> >> selection2 <- my.match.fun(selection2) ###
>> >>
>> >> return(aggregate(data1, as.yearqtr,
>> selection2))
>> >> }
>> >>
>> >> fnc(data1)
>> >
>> >
>> >
>> >
>
>
>
>