I am having the following error in my function function(theta,reqdIRR) { theta1<-theta[1] theta2<-theta[2] n<-length(reqdIRR) constant<- n*(theta1+theta2) sum1<-lapply(reqdIRR*exp(theta1),FUN = sum) sum2<-lapply(exp(theta2 - reqdIRR*exp(theta1)),FUN = sum) sum = sum1 + sum2 log.fcn = constant - as.numeric(sum) result = - log.fcn return(result) } *error : neg.log.gumbel(1,reqdIRR) Error in sum1 + sum2 : non-numeric argument to binary operator>how can i rectify the error?Its really urgent... * [[alternative HTML version deleted]]
Kakul lapply() returns a list - as stated in the help file (in general, the help files and documentation provided with R are very good once one gets used to them). Lists are, potentially, complex things and it makes no sense to add them. Presumably in your case it does make sense to add them - in which case you must first coerce them into another form, or use a function other than lapply() in the first place. Which might be the better option depends upon the nature of reqdIRR. HTH ......... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of kakul modani > Sent: Tuesday, 22 April 2008 1:41 p.m. > To: r-help at r-project.org > Subject: [R] how to convert non numeric data into numeric? > > I am having the following error in my function > > function(theta,reqdIRR) > { > theta1<-theta[1] > theta2<-theta[2] > n<-length(reqdIRR) > constant<- n*(theta1+theta2) > sum1<-lapply(reqdIRR*exp(theta1),FUN = sum) > sum2<-lapply(exp(theta2 - reqdIRR*exp(theta1)),FUN = sum) sum > = sum1 + sum2 log.fcn = constant - as.numeric(sum) result = - log.fcn > return(result) > } > > *error : neg.log.gumbel(1,reqdIRR) > Error in sum1 + sum2 : non-numeric argument to binary operator > > > > how can i rectify the error?Its really urgent... > > * > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >The contents of this e-mail are privileged and/or confidential to the named recipient and are not to be used by any other person and/or organisation. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail.
"kakul modani" <kakulmodani at gmail.com> wrote in news:91ad84370804211840y7514d720l6d119f9255c0b362 at mail.gmail.com:> I am having the following error in my function > > function(theta,reqdIRR) > { > theta1<-theta[1] > theta2<-theta[2] > n<-length(reqdIRR) > constant<- n*(theta1+theta2) > sum1<-lapply(reqdIRR*exp(theta1),FUN = sum) > sum2<-lapply(exp(theta2 - reqdIRR*exp(theta1)),FUN = sum) > sum = sum1 + sum2 > log.fcn = constant - as.numeric(sum) > result = - log.fcn > return(result) > } > > *error : neg.log.gumbel(1,reqdIRR) > Error in sum1 + sum2 : non-numeric argument to binary operator >> > > how can i rectify the error?Its really urgent...sum1 and sum2 are lists and "+" is expecting a vector. Two choices --- substituting: sum = unlist(sum1)+ unlist(sum2) --or-- change the lapply(...) to sapply(...) which will return a numeric vector. -- David Winsemius
*@ Mr. Alspach and @ Mr. Winsemius : Thanks for replying. reqdIRR contains returns in lognormal form.I am trying to estimate parameter for type-1 Gumbel Distribution and fit it into reqdIRR. If i use sapply its returning me NA values. * I am having the following error in my function function(theta,reqdIRR) { theta1<-theta[1] theta2<-theta[2] n<-length(reqdIRR) constant<- n*(theta1+theta2) sum1<-lapply(reqdIRR*exp (theta1),FUN = sum) sum2<-lapply(exp(theta2 - reqdIRR*exp(theta1)),FUN = sum) sum = sum1 + sum2 log.fcn = constant - as.numeric(sum) result = - log.fcn return(result) } *error : neg.log.gumbel(1,reqdIRR) Error in sum1 + sum2 : non-numeric argument to binary operator>how can i rectify the error?Its really urgent... * [[alternative HTML version deleted]]
David Winsemius
2008-Apr-22 03:46 UTC
[R] Fwd: how to convert non numeric data into numeric?
"kakul modani" <kakulmodani at gmail.com> wrote in news:91ad84370804211946i4ddf7f49i5116ee7eaf5204c1 at mail.gmail.com:> *@ Mr. Alspach and @ Mr. Winsemius : Thanks for replying. > > reqdIRR contains returns in lognormal form.I am trying to estimate > parameter for type-1 Gumbel Distribution and fit it into reqdIRR. > > If i use sapply its returning me NA values.Not for me. I think it is time to heed the posting guide. Minimal example with data and function call needed before I expend any more time in guesswork. Perhaps you called that function with a scalar for theta rather than a vector with two elements. I have no idea whether the code is correct, but it is not throwing any errors when I invoke it. -------working function -----------> tI <- function(theta,reqdIRR)+ { + theta1<-theta[1] + theta2<-theta[2] + n<-length(reqdIRR) + constant<- n*(theta1+theta2) + sum1<-sapply(reqdIRR*exp (theta1),FUN = sum) + sum2<-sapply(exp(theta2 - reqdIRR*exp(theta1)),FUN = sum) + sum = sum1 + sum2 + log.fcn = constant - as.numeric(sum) # probably do not need the as.numeric # pretty sure it already is + result = - log.fcn + return(result) + }> > e.norm <- exp(rnorm(10)) > theta <- c(1,2) > tI(theta, e.norm)[1] -2.136269 -16.083672 -23.989528 -24.454704 -13.882977 [6] -26.686971 -26.305476 -26.869672 -25.927103 -23.307188 --------------- -- David Winsemius> * > I am having the following error in my function > > function(theta,reqdIRR) > { > theta1<-theta[1] > theta2<-theta[2] > n<-length(reqdIRR) > constant<- n*(theta1+theta2) > sum1<-lapply(reqdIRR*exp (theta1),FUN = sum) > sum2<-lapply(exp(theta2 - reqdIRR*exp(theta1)),FUN = sum) > sum = sum1 + sum2 > log.fcn = constant - as.numeric(sum) > result = - log.fcn > return(result) > } > > *error : neg.log.gumbel(1,reqdIRR) > Error in sum1 + sum2 : non-numeric argument to binary operator >> > > how can i rectify the error?Its really urgent... > > * > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html and provide commented, > minimal, self-contained, reproducible code. > >