# [R] R 3.0.2 - How to create intervals and group another variable in those intervals?

jim holtman jholtman at gmail.com
Wed Nov 6 18:22:50 CET 2013

```should of had in the script:

int <- seq(0, max(x\$speed) + 4.5, by = 4.5)

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

On Wed, Nov 6, 2013 at 12:19 PM, jim holtman <jholtman at gmail.com> wrote:
> Is this what you are after:
>
>
>> n <- 1000
>> x <- data.frame(speed = runif(n, 0, 85.53)
> +             , spacing = rnorm(n)
> +             )
>> # create interval for speed
>> int <- seq(0, max(x) + 4.5, by = 4.5)
>> # split the data and find average of spacing
>> tapply(x\$spacing, cut(x\$speed, int), mean)
>     (0,4.5]     (4.5,9]    (9,13.5]   (13.5,18]   (18,22.5]
> (22.5,27]   (27,31.5]   (31.5,36]
>  0.27840783 -0.08349567 -0.10659408 -0.01476840 -0.08773255
> -0.06643826  0.21873016  0.17627232
>   (36,40.5]   (40.5,45]   (45,49.5]   (49.5,54]   (54,58.5]
> (58.5,63]   (63,67.5]   (67.5,72]
> -0.16568350 -0.15458191 -0.04909331 -0.01179396  0.25022296
> -0.27553812 -0.14927483 -0.21000177
>   (72,76.5]   (76.5,81]   (81,85.5]
> -0.09884137 -0.08459709  0.02864456
>>
>>
>
> plotting is left as an exercise for the reader, but given the averages
> above, you can find the midpoints easily.
>
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?
> Tell me what you want to do, not how you want to do it.
>
>
> On Wed, Nov 6, 2013 at 10:48 AM, umair durrani <umairdurrani at outlook.com> wrote:
>> I have two columns for speed ('Smoothed velocity') and Spacing. What I want to do is to first create the intervals of speed (minimum value=0, max value= 85.53), group the Spacing values falling in a particular Speed interval, find the average of the Spacing for an interval and finally plot the average spacing of each interval against the mid-point of the Speed interval. I want to have fixed intervals of 4.5 feet per second, i.e. 0-4.5, 4.5-9,......xx-85.53.After hours of search I found a function for creating intervals called classIntervals() but I can't figure out how to create fixed intervals of 4.5. Here is what I tried:classIntervals(s21[,'Smoothed velocity'], style='fixed', fixedBreaks=4.5)But the results were unexpected and there was a Warning message:In classIntervals(s21[, "Smoothed velocity"], style = "fixed", fixedBreaks = 4.5) :
>>   variable range greater than fixedBreaksEven after intervals are created, I need to group spacing and find the avg. for every interval. How can I do this? I have tried what I could, please help
>>
>> Umair Durrani
>>
>> email: umairdurrani at outlook.com
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help