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))
>               }
>       })
>       return(storage)
>       }
> 
> but I end up with this error when I try and use the above function in
> tapply():
> 
> Error in ans[!test & !nas] <- rep(no, length.out = length(ans))[!test & : 
> replacement has length zero
> 
> 

ifelse is for vector arguments.
You should use if(....) {.......} else {.....}

But why not just

c(x[1], diff(x))/max(x)

Berend

--
View this message in context: 
http://r.789695.n4.nabble.com/error-in-self-made-function-cannot-deal-with-objects-of-length-1-tp3710555p3710621.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@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.

Reply via email to