[R] Aggregate by minimum

jim holtman
Tue, 10 Jan 2012 02:00:58 -0000

try this:

> x <- structure(list(speed = c(3,9,14,8,7,6), result = 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),
+    id = c("1000", "10000",
+    "10001", "10002", "10003",
"10004")), .Names = c("speed",
+ "result", "house", "date", "id"),
class = "data.frame", row.names = c("1000",
+ "10000", "10001", "10002", "10003",
"10004"))
>
> require(plyr)
> ddply(x, .(date), .fun = function(a){
+     a[which.min(a$speed), ]
+ })
  speed result house date    id
1     3  0.697     1  719  1000
2     8  0.012     1 1027 10002
3     6  0.019     1 1030 10004
>
>

On Mon, Jan 9, 2012 at 8:23 PM, Jeffrey Joh <johjeffrey at hotmail.com>
wrote:
>
>
>
>
> For each date, I would like to keep the line with the lowest speed and
discard the rest. ?The result would have one line for each date. ?Can I use
aggregate for this?
>
> My table is as follows:
> structure(list(speed = c(3,9,14,8,7,6), result = 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),
> ? ?id = c("1000", "10000",
> ? ?"10001", "10002", "10003",
"10004")), .Names = c("speed",
> "result", "house", "date", "id"),
class = "data.frame", row.names = c("1000",
> "10000", "10001", "10002", "10003",
"10004"))
>
> 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.



--
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.