r - Computing ranges within bins -


i'm trying find sum of each bin given random vector, code returning first element of vector 100. how cycle through each of elements in vector x, check if range of bin j, , return sum each bin?

i realize there functions in r, i'm working on hard coding specific example.

# sample data set.seed(1234) x <- rnorm(100)   s <- range(x) <- range(x)[1] b <- range(x)[2] j <- 5    #bins h <- (b - a)/j   #interval  (j in 1:j){   (n in 1:length(x)){     ifelse(x[n] > + (j-1)*h & (x[n] <= + j*h), n[j] <- n[j] + 1, n[j] <- n[j] + 0)   } } 

output:

> n [1] 100  na  na  na  na 

desired output:

> n [1]  7 43 29 13  8 

why not use cut , table?

set.seed(1234) x <- rnorm(100) bin <- cut(x, breaks = 5)    ## evenly cut `range(x)` 5 bins levels(bin) # [1] "(-2.35,-1.37]"  "(-1.37,-0.388]" "(-0.388,0.591]" "(0.591,1.57]"   # [5] "(1.57,2.55]"   table(bin) # (-2.35,-1.37] (-1.37,-0.388] (-0.388,0.591]   (0.591,1.57]    (1.57,2.55]  #             7             43             29             13              8 

still, need show why loop fails. note don't need ifelse; ordinary if (...) ... sufficient. error used n loop index, use record counts! following corrects this, using new vector counts distinguish n:

counts <- integer(j)  ## initialization (j in 1:j){   (n in 1:length(x)) {     if (x[n] > + (j-1)*h && x[n] <= + j*h) counts[j] <- counts[j] + 1l     }   }  counts # [1]  6 43 29 13  7 

perhaps have noted first value 6 not 7. because loop condition x[n] > + (j-1)*h && x[n] <= + j*h not include lowest value first bin. since case, need manually add 1 counts[1].


Comments