Alex Reynolds -4 years ago 97

R Question

I have a set of levels in R that I generate with

`cut`

`> frac <- cut(c(0, 1), breaks=10)`

> levels(frac)

[1] "(-0.001,0.1]" "(0.1,0.2]" "(0.2,0.3]" "(0.3,0.4]" "(0.4,0.5]"

[6] "(0.5,0.6]" "(0.6,0.7]" "(0.7,0.8]" "(0.8,0.9]" "(0.9,1]"

Given a vector

`v`

`[0.0, 1.0]`

`v`

`levels(frac)`

I could customize the number of breaks and/or the interval from which I am making levels, so I'm looking for a way to do this with standard R commands, so that I can build a two-column data frame: one column for the levels as factors, and the second column for a fractional or percentage value of total elements in

`v`

Note: The following does not work:

`> table(frac)`

frac

(-0.001,0.1] (0.1,0.2] (0.2,0.3] (0.3,0.4] (0.4,0.5] (0.5,0.6]

1 0 0 0 0 0

(0.6,0.7] (0.7,0.8] (0.8,0.9] (0.9,1]

0 0 0 1

If I use

`cut`

`v`

`cut`

My goal is to take different vectors and bin them to the same set of levels. Hopefully this helps clarify my question. Thanks for any assistance.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

```
frac = seq(0,1,by=0.1)
ranges = paste(head(frac,-1), frac[-1], sep=" - ")
freq = hist(v, breaks=frac, include.lowest=TRUE, plot=FALSE)
data.frame(range = ranges, frequency = freq$counts)
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**

Latest added