umair durrani
2013-Nov-06 15:48 UTC
[R] R 3.0.2 - How to create intervals and group another variable in those intervals?
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@outlook.com [[alternative HTML version deleted]]
jim holtman
2013-Nov-06 17:19 UTC
[R] R 3.0.2 - How to create intervals and group another variable in those intervals?
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 > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
jim holtman
2013-Nov-06 18:03 UTC
[R] R 3.0.2 - How to create intervals and group another variable in those intervals?
You need to read up on the use of the "apply" functions: this is just another 'tapply' call with the 'speed' in it.> 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.260755737 -0.017766405 -0.097255963 0.234719308 -0.038908267 -0.004559798 -0.065556109 -0.144327936 (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.135491878 -0.120387573 0.033821289 -0.110058896 -0.107173009 -0.091258106 -0.198911676 -0.138334909 (72,76.5] (76.5,81] (81,85.5] -0.127941501 0.198943106 0.136345405> > # find the average speed > tapply(x$speed, 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] (36,40.5] (40.5,45] 2.276792 6.804097 11.024388 15.699358 20.184667 24.644743 29.050843 34.017265 38.288514 42.617572 (45,49.5] (49.5,54] (54,58.5] (58.5,63] (63,67.5] (67.5,72] (72,76.5] (76.5,81] (81,85.5] 47.357247 51.808874 56.162820 60.719576 65.202813 70.055034 74.147431 79.070495 83.376027>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:36 PM, umair durrani <umairdurrani at outlook.com> wrote:> Thanks Jim. You now provided me with the average of spacing but how can I > find the average of speed for every speed interval? I am a newbie in R, > sorry for my stupid questions > > > Umair Durrani > email: umairdurrani at outlook.com > > >> Date: Wed, 6 Nov 2013 12:22:50 -0500 >> Subject: Re: [R] R 3.0.2 - How to create intervals and group another >> variable in those intervals? >> From: jholtman at gmail.com >> To: umairdurrani at outlook.com >> CC: r-help at r-project.org >> >> 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 >> >> PLEASE do read the posting guide >> >> http://www.R-project.org/posting-guide.html >> >> and provide commented, minimal, self-contained, reproducible code.