r - error when using which.min() with ave() -
r - error when using which.min() with ave() -
i have data, much of na
. simplicity, let's looks this:
x = c(na, 3, 4, 3.5, na, na, na, na, 7, 5) bins = c(1, 1, 1, 2, 2, 2, 3, 3, 4, 4)
i'm using ave( )
, which.min( )
minimum value each bin type:
ave(x, segments, fun = which.min)
but error:
error in `split<-.default`(`*tmp*`, g, value = lapply(split(x, g), fun)) : replacement has length 0
the reason happening (i think) because bin # 3 has na
values. when rectified, error disappears. utilize function like:
ave(x, segments, fun = function(xx){ if(all(is.na(xx))){ return(na) } else { xx = which.min(xx) return(xx) }} )
but:
1) hacky heck. and
2) which.min(c(na, na, na))
not cause error, nor ave(c(na, na, na), c(1, 1, 1), fun=mean)
- what's going on i'm missing?
--> have thought of why error happens / best way around it?
cheers.
r
Comments
Post a Comment