[R] Calculating differences

R. Michael Weylandt michael.weylandt at gmail.com
Fri Oct 21 00:05:50 CEST 2011


I'd dont quite get what you are asking, but here's my best guess and
you can tweak it go get what you need.

I'd do it in two passes since you want two rather unrelated objects.

structure(list(speed = c(3,9,4,8,7,6), C = c(0.697, 0.011, 0.015,
0.012, 0.018, 0.019), house = c(1,
1, 1, 1, 1, 1), date = c(719, 1027, 1027, 1027, 1030, 1030),
   hour = c(18, 8, 8, 8, 11, 11), id = c("1000", "10000",
   "10001", "10002", "10003", "10004")), .Names = c("speed",
"C", "house", "date", "hour", "id"), class = "data.frame", row.names = c("1000",
"10000", "10001", "10002", "10003", "10004")) -> X

tapply(X, X$date, function(d) d$speed - min(d$speed))
tapply(X, X$date, function(d) d$C[which.min(d$speed)])

Michael


On Thu, Oct 20, 2011 at 5:56 PM, Jeffrey Joh <johjeffrey at hotmail.com> wrote:
>
> I have a table that looks like this:
>
> structure(list(speed = c(3,9,4,8,7,6), C = c(0.697, 0.011, 0.015, 0.012, 0.018, 0.019), house = c(1,
> 1, 1, 1, 1, 1), date = c(719, 1027, 1027, 1027, 1030, 1030),
>    hour = c(18, 8, 8, 8, 11, 11), id = c("1000", "10000",
>    "10001", "10002", "10003", "10004")), .Names = c("speed",
> "C", "house", "date", "hour", "id"), class = "data.frame", row.names = c("1000",
> "10000", "10001", "10002", "10003", "10004"))
>
> I want to determine the minimum speed for each date, and the C that corresponds to that lowest speed.Then I want to make a table that contains all speeds and the difference between C and the lowest C.
>
> For example, on the date 1027, the minimum speed is 4 and the C that corresponds to that is 0.015.  The new table should contain:
> speed 8 and C -0.003
> speed 9 and C -0.004
> speed 7 and C -0.001How do you do this?
>
> Thanks,
> Jeffrey
>
>        [[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.
>



More information about the R-help mailing list