如何按日期订购日期:时间格式,然后按非数字变量

时间:2015-08-06 17:55:45

标签: r

我想首先通过非数字帐户变量按日期订购my.data。按日期强制相同的帐户。示例

My.data <- structure(list(Account = c("4Z123445", "2345678", "1234567","4Z123445"), dateofEntry = c("2015-06-07:03-16-43", "2015-07-27:02-12-02", "2015-06-21:01-56-15", "2015-07-06:18-04-30"),
                      Message = c("Pass", "Fail", "Pass", "Fail")),
                      .Names = c("Account", "dateofEntry", "Message" ), 
                 class = "data.frame", row.names = c(NA, -4L))

My.data
Account    dateofEntry           Message
4Z123445   2015-06-07:03-16-43   Pass
2345678    2015-07-27:02-12-02   Fail
1234567    2015-06-21:01-56-15   Pass
4Z123445   2015-07-06:18-04-30   Fail

清理后的数据类似于:

My.data2
Account    dateofEntry           Message
1234567    2015-06-21:01-56-15   Pass
2345678    2015-07-27:02-12-02   Fail
4Z123445   2015-06-07:03-16-43   Pass  
4Z123445   2015-07-06:18-04-30   Fail

3 个答案:

答案 0 :(得分:2)

order中的data.table很容易:

library(data.table)
> setDT(My.data)[order(Account,dateofEntry)]
    Account         dateofEntry Message
1:  1234567 2015-06-21:01-56-15    Pass
2:  2345678 2015-07-27:02-12-02    Fail
3: 4Z123445 2015-06-07:03-16-43    Pass
4: 4Z123445 2015-07-06:18-04-30    Fail

如果您确实希望先按日期排序,则可以简单地撤消订单:

setDT(My.data)[order(dateofEntry,Account)]
    Account         dateofEntry Message
1: 4Z123445 2015-06-07:03-16-43    Pass
2:  1234567 2015-06-21:01-56-15    Pass
3: 4Z123445 2015-07-06:18-04-30    Fail
4:  2345678 2015-07-27:02-12-02    Fail

答案 1 :(得分:1)

library(dplyr)
arrange(My.data, Account, dateofEntry)

答案 2 :(得分:1)

如果我正确地提出了您的问题,以下代码应该这样做:

My.data[order(My.data[,1], My.data[,2]),]
   Account         dateofEntry Message
3  1234567 2015-06-21:01-56-15    Pass
2  2345678 2015-07-27:02-12-02    Fail
1 4Z123445 2015-06-07:03-16-43    Pass
4 4Z123445 2015-07-06:18-04-30    Fail