简体   繁体   English

在R中对字符向量进行排序

[英]sort character vectors in R

I have this vector that I need to sort by descending order. 我有这个向量,我需要按降序排序。 The latest txt in the first place: 首先是最新的txt:

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, ie hour_minute-month_day_year. 我应该能够通过文本“ 11_25-01_20_2015”中的此条目对此进行排序,其中11是小时, 25分钟, 01个月, 20天和2015年,即hour_minute-month_day_year。

How could I do this? 我该怎么办?

If the end of the strings are consistent (bla-bla-bla- time-date.txt ), you may use substring to extract the times. 如果字符串的末尾是一致的(bla-bla-bla- time-date.txt ),则可以使用substring提取时间。 Then convert times to as.POSIXct and use them in order 然后将时间转换为as.POSIXct并按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"

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"

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)]

You could potentially extract the dates, convert to POSIXct class and then get the latest date using which.max 您可能会提取日期,将其转换为POSIXct类,然后使用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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM