1

I have a dataset something like this

sample value
  A     0.2
  B     0.25
  C     0.51
  D     0.91

I would like to bin the data in the following custom bin of c(0, 0.25, 0.50, 0.75, 0.9) and find out the freq of 'sample' falling in the bin. Using the new binned data, I would like to draw a histogram of values against the number of samples. Any help is appreciated to help me produce these custom bins

Ira
  • 99
  • 5
  • Try this `df$Cut <- cut(df$value,breaks = c(0, 0.25, 0.50, 0.75, 0.9,Inf),include.lowest = T,right = T,dig.lab = 10)` – Duck Sep 11 '20 at 14:54

1 Answers1

1

Maybe you can try table+ cut

bin <- c(0, 0.25, 0.50, 0.75, 0.9)

> table(cut(df1$value,c(bin,Inf)))

  (0,0.25] (0.25,0.5] (0.5,0.75] (0.75,0.9]  (0.9,Inf] 
         2          0          1          0          1

or hist

> hist(df1$value,breaks = c(bin,Inf), plot = FALSE)
$breaks
[1] 0.00 0.25 0.50 0.75 0.90  Inf

$counts
[1] 2 0 1 0 1

$density
[1] 2 0 1 0 0

$mids
[1] 0.125 0.375 0.625 0.825   Inf

$xname
[1] "df1$value"

$equidist
[1] FALSE

attr(,"class")
[1] "histogram"
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • Many thanks. All the above worked. I made the histogram with the output ('df1') usin above methods, like so, hist(as.numeric(df1$value),plot = TRUE) – Ira Sep 11 '20 at 15:04