Reputation: 10626
I have this vector that I need to sort by descending order. The latest txt in the first place:
d<-c("/SiteScope/accounts/login59/htdocs/Reports-1722992141/Report-02_06-01_04_2015.txt","/SiteScope/accounts/login59/htdocs/Reports-1722992141/Report-02_06-01_11_2015.txt","/SiteScope/accounts/login59/htdocs/Reports-1722992141/Report-02_06-01_18_2015.txt","/SiteScope/accounts/login59/htdocs/Reports-1722992141/Report-02_08-01_25_2015.txt","/SiteScope/accounts/login59/htdocs/Reports-1722992141/Report-11_25-01_20_2015.txt")
when I do this:
d <- sort(d)
d[1]
# "/SiteScope/accounts/login59/htdocs/Reports-1722992141/Report-02_06-01_04_2015.txt"
It needs to be this:
"/SiteScope/accounts/login59/htdocs/Reports-1722992141/Report-02_08-01_25_2015.txt"
I should be able to sort this by this entry in the text "11_25-01_20_2015", where 11
are hours, 25
minutes, 01
months, 20
days, and 2015
years, i.e. hour_minute-month_day_year.
How could I do this?
Upvotes: 1
Views: 126
Reputation: 92282
You could potentially extract the dates, convert to POSIXct
class and then get the latest date using which.max
library(stringi)
indx <- as.POSIXct(stri_extract_first_regex(d, "(?<=Report-).*(?=\\.txt)"), format = "%H_%M-%m_%d_%Y")
d[which.max(indx)]
# [1] "/SiteScope/accounts/login59/htdocs/Reports-1722992141/Report-02_08-01_25_2015.txt"
Or you can just order in decreasing order
d[order(indx, decreasing = TRUE)]
# [1] "/SiteScope/accounts/login59/htdocs/Reports-1722992141/Report-02_08-01_25_2015.txt"
# [2] "/SiteScope/accounts/login59/htdocs/Reports-1722992141/Report-11_25-01_20_2015.txt"
# [3] "/SiteScope/accounts/login59/htdocs/Reports-1722992141/Report-02_06-01_18_2015.txt"
# [4] "/SiteScope/accounts/login59/htdocs/Reports-1722992141/Report-02_06-01_11_2015.txt"
# [5] "/SiteScope/accounts/login59/htdocs/Reports-1722992141/Report-02_06-01_04_2015.txt"
Upvotes: 2
Reputation: 67778
If the end of the strings are consistent (bla-bla-bla-time-date.txt), you may use substring
to extract the times. Then convert times to as.POSIXct
and use them in order
time <- substring(d, first = nchar(d)-19)
d[order(as.POSIXct(time, format = "%H_%M-%m_%d_%Y.txt"), decreasing = TRUE)]
# [1] "/SiteScope/accounts/login59/htdocs/Reports-1722992141/Report-02_08-01_25_2015.txt"
# [2] "/SiteScope/accounts/login59/htdocs/Reports-1722992141/Report-11_25-01_20_2015.txt"
# [3] "/SiteScope/accounts/login59/htdocs/Reports-1722992141/Report-02_06-01_18_2015.txt"
# [4] "/SiteScope/accounts/login59/htdocs/Reports-1722992141/Report-02_06-01_11_2015.txt"
# [5] "/SiteScope/accounts/login59/htdocs/Reports-1722992141/Report-02_06-01_04_2015.txt"
Upvotes: 5
Reputation: 66834
First you should extract the times and put them in a sensible format:
times <- as.POSIXct(sub("^.+Report-([0-9]+)_([0-9]+)-([0-9]+)_([0-9]+)_([0-9]+)\\.txt$","\\5-\\3-\\4 \\1:\\2",d))
times
[1] "2015-01-04 02:06:00 GMT" "2015-01-11 02:06:00 GMT"
[3] "2015-01-18 02:06:00 GMT" "2015-01-25 02:08:00 GMT"
[5] "2015-01-20 11:25:00 GMT"
Then you can use these to order your original data:
d[order(times, decreasing=TRUE)][1]
[1] "/SiteScope/accounts/login59/htdocs/Reports-1722992141/Report-02_08-01_25_2015.txt"
Upvotes: 3
Reputation: 10167
Try this:
# trim everythin before the string 'Report-'
dateSting <- gsub('^.*Report-','',d )
# trim the '.txt' from the end.
dateSting <- gsub('\\.txt$','',dateSting )
#convert the date string to a date-time object
dateTime <- as.POSIXct(dateSting,'%H_%M-%m_%d_%Y')
# sort on date time
d <- d[order(dateTime)]
Upvotes: 2