[英]Finding the minimum value of a variable for each value of another variable in R
I'm fairly new to R and am trying to find the minimum date/time for each value of an ID number. 我对R相当陌生,正在尝试查找ID号每个值的最小日期/时间。 Below is an example of the data I'm working with
以下是我正在处理的数据的示例
ID DATE
1 11/24/12 12:51
1 11/24/12 12:52
1 11/24/12 12:53
2 11/27/12 12:51
2 11/24/12 12:52
2 11/24/12 12:53
What I need to do is generate an object that shows the earliest date/time for each value of ID like so: 我需要做的是生成一个对象,该对象显示每个ID值的最早日期/时间,如下所示:
ID DATE
1 11/24/12 12:51
2 11/27/12 12:51
I've tried several approaches but am still struggling. 我尝试了几种方法,但仍在努力。
Any suggestions would be appreciated! 任何建议,将不胜感激!
Try this (as Roland suggests) using R base functions 尝试使用R基函数(如Roland建议的那样)
DATE <- strptime(c("11/24/12 12:51", "11/24/12 12:52", "11/24/12 12:53",
"11/27/12 12:51", "11/24/12 12:52", "11/24/12 12:53"),
"%m/%d/%y %H:%M")
ID <- rep(1:2, each=3)
DF <- data.frame(ID, DATE)
aggregate(DATE ~ ID, min, data=DF)
ID DATE
1 1 2012-11-24 12:51:00
2 2 2012-11-24 12:52:00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.