您可以對列表名稱和集合級別進行因子分解,但我假設您有許多不同的年份和月份組合列表。您可以使用內置的不斷month.abb得到相應的指數,見下圖:
# sample list
byMonthList <- list()[rep(1,11)]
names(byMonthList) <- c("2004.Apr", "2004.Aug", "2004.Dec", "2004.Feb", "2004.Jul", "2004.Jun", "2004.Mar", "2004.May", "2004.Nov", "2004.Oct", "2004.Jan")
# split month name from year.month names using strsplit
# extract month name using sapply
ls_month <- sapply(strsplit(names(byMonthList), split="\\."), function(x) x[2])
ls_month
# [1] "Apr" "Aug" "Dec" "Feb" "Jul" "Jun" "Mar" "May" "Nov" "Oct" "Jan"
# use match and built in constant month.abb to get numeric value of months
ls_month_num <- match(ls_month, month.abb)
ls_month_num
# [1] 4 8 12 2 7 6 3 5 11 10 1
# Use ls_month_num to reorder your list
byMonthList[names(byMonthList)[order(ls_month_num)]]
# Or
byMonthList[names(byMonthList)[order(match(sapply(strsplit(names(byMonthList), split="\\."), function(x) x[2]), month.abb))]]
這不回答這個問題。 as.factor函數沒有日期排序功能。 –