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]]
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.
>
Hi:
Here's one way with the plyr package. Using ds as the name of your
data frame (thank you for the dput and clear description of what you
wanted, BTW),
library('plyr')
ddply(ds, .(date), mutate, minspd = min(speed), Cmin C[which.min(speed)], diff =
C - Cmin)
speed C house date hour id minspd Cmin diff
1 3 0.697 1 719 18 1000 3 0.697 0.000
2 9 0.011 1 1027 8 10000 4 0.015 -0.004
3 4 0.015 1 1027 8 10001 4 0.015 0.000
4 8 0.012 1 1027 8 10002 4 0.015 -0.003
5 7 0.018 1 1030 11 10003 6 0.019 -0.001
6 6 0.019 1 1030 11 10004 6 0.019 0.000
HTH,
Dennis
On Thu, Oct 20, 2011 at 2: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.
>