What - 1 year ago 70

R Question

I am turning my variables from discrete to continuous one. How can i now get the median values for each of my intervals.

`sample <- c(30,16,12,40,14,30,5,5,5,5,38,30,45,22,30,31,22,35,45,29)`

sample$Cut <- cut(sample,seq(min(sample),max(sample),(max(sample)-min(sample))/r),include.lowest = TRUE)

where my magical r value is

`r = log(length(sample), base = 2)`

r = trunc(r)

r = r + 1

Now i need to get median values from each of this intervals:

[5,13] (13,21] (21,29] (29,37] (37,45]

For this example i have to get 9,17,25,33,41.

Sorry for so silly question. Thank you in advance.

Answer Source

You have break points:

```
b <- seq(min(sample),max(sample),(max(sample)-min(sample))/r)
# [1] 5 13 21 29 37 45
```

therefore the mid points for those intervals are:

```
(b[-1] + b[-length(b)]) / 2
# [1] 9 17 25 33 41
```