[R] error in self-made function - cannot deal with objects of length = 1
Berend Hasselman
bhh at xs4all.nl
Mon Aug 1 21:47:22 CEST 2011
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.
More information about the R-help
mailing list