-3

I would like to First order my.data by date thne by the non numeric account variable. Forcing the same accounts together by date. Example

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

Cleaned data would be something like:

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
Ally Kat
  • 191
  • 2
  • 2
  • 10

3 Answers3

2

This is easy with order in 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

You can simply reverse the order if you really do want to sort by date first:

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
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
  • 1
    thank you. I am moving from SAS to R and it's all quite new. With self teaching. You *all* on this forum have been great! – Ally Kat Aug 07 '15 at 13:04
  • @AllyKat were especially happy to help converts! you'll get the hang of it soon enough :) – MichaelChirico Aug 07 '15 at 13:13
  • Here is a quick-start [cheatsheet](https://github.com/rstudio/cheatsheets/raw/master/datatable.pdf) for `data.table`. If you're interested, here are some [starter's guides](https://github.com/Rdatatable/data.table/wiki/Getting-started). – MichaelChirico Apr 18 '19 at 02:43
1
library(dplyr)
arrange(My.data, Account, dateofEntry)
Benjamin
  • 16,897
  • 6
  • 45
  • 65
1

If I get your question correctly the following code should do it:

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
Curious T
  • 160
  • 1
  • 6