Re: [R] error in self-made function - cannot deal with objects of length = 1

2011-08-01 Thread R. Michael Weylandt
But if you do mean to divide by max(x), I'll also vote for the prior ROI <- function(x) { if (length(x)==1) return(NA) r=c(x[1], diff(x))/max(x) return(r)} As being about as quick and elegant as this can be done in R. M On Aug 1, 2011, at 4:07 PM, "R. Michael Weylandt " wrote: > Just jumpin

Re: [R] error in self-made function - cannot deal with objects of length = 1

2011-08-01 Thread bjmjarrett
> But why not just > c(x[1], diff(x))/max(x) So simple! Thank you ever so much Berend. Best wishes, Ben -- View this message in context: http://r.789695.n4.nabble.com/error-in-self-made-function-cannot-deal-with-objects-of-length-1-tp3710555p3710646.html Sent from the R help mailing list a

Re: [R] error in self-made function - cannot deal with objects of length = 1

2011-08-01 Thread R. Michael Weylandt
Just jumping into this, but does the ROC(x, type="discrete") function of either the TTR or caTools (can't remember which) work if you need a prebuilt function? Also, why are you dividing by the max value? That seems a funny way to calculate ROC... On Aug 1, 2011, at 3:14 PM, bjmjarrett wrote:

[R] error in self-made function - cannot deal with objects of length = 1

2011-08-01 Thread bjmjarrett
I have a function to calculate the rate of increase (the difference between the value and the previous value divided by the total number of eggs in a year) of egg production over the course of a year: rate <- function(x){ storage <- matrix(nrow=length(x),ncol=1) storage[1,] <- x[1] /

Re: [R] error in self-made function - cannot deal with objects of length = 1

2011-08-01 Thread Berend Hasselman
bjmjarrett wrote: > > ... > rate <- function(x){ > storage <- matrix(nrow=length(x),ncol=1) > ifelse(length(x)==1,storage[1,] <- NA,{ > storage[1,] <- x[1]/max(x) > for(i in 2:length(x)){ > p <- i-1 > storage[i,] <- ((x[i] - x[p]) / max(x)) >