首页 > 解决方案 > 重新排序名称包含日期顺序的数据框列?

问题描述

我有一个反应性数据框,其中列名更改并且名称列Month.Year乱序。如何将第一个 Month.Year 放在“Current”之后最左边的位置?以下是数据框列的排序方式以及我希望它们的排序方式。

print(colnames(df))
#[1] "ProductCategoryDesc" "RegionDesc"          "SourceDesc"          "Report"             
#[5] "Apr.2019"            "Current"             "Feb.2019"            "Jun.2019"           
#[9] "Mar.2019"            "May.2019"            "Mar.2020"

#the order I want is below
#[1] "ProductCategoryDesc" "RegionDesc"          "SourceDesc"          "Report"             
#[5] "Current"             "Feb.2019"             "Mar.2019"            "Jun.2019"           
#[9] "Apr.2019"            "May.2019"             "Mar.2020"

#####################################################################
#another example of the df
print(colnames(df))

#[1] "ProductCategoryDesc" "RegionDesc"          "SourceDesc"          "Report"             
#[5] "Apr.2019"            "Current"             "Feb.2019"            "Jun.2019"           
#[9] "Mar.2019"            "May.2019"            "Sep.2019"

#the order I want is below
#[1] "ProductCategoryDesc" "RegionDesc"          "SourceDesc"          "Report"             
#[5] "Current"             "Feb.2019"             "Mar.2019"            "Apr.2019"           
#[9] "May.2019"            "Jun.2019"             "Sep.2019"

这是有关 df 外观的一些信息

print(dput(droplevels(head(d3))))
#below is the output

structure(list(ProductCategoryDesc = structure(c(1L, 1L, 1L, 
1L, 1L, 1L), .Label = "CN AMMONIA", class = "factor"), RegionDesc = 
structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = "AB REG 2 UPPER MIDWEST", class = "factor"), 
SourceDesc = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "CN-SD, WATERTOWN 
LIQUID", class = "factor"), 
Report = structure(1:6, .Label = c("InventoryAvailabletoShip", 
"NetCashPosition", "NetMarketPositionTotal", "NonDirectShipPurchase", 
"TotalDirectShips", "TotalNonDirectShips"), class = "factor"), 
Apr.2019 = c(0, 0, 0, 0, 0, 0), Current = c(0, 0, 0, 0, 0, 
0), Feb.2019 = c(0, 0, 240, 240, 0, 240), Jun.2019 = c(0, 
0, 0, 0, 0, 0), Mar.2019 = c(0, 0, 0, 0, 0, 0), May.2019 = c(0, 
0, 0, 0, 0, 0)), sorted = c("ProductCategoryDesc", "RegionDesc", 
"SourceDesc", "Report"), row.names = c(NA, -6L), .internal.selfref = 
<pointer: 0x0000000000211ef0>, class = c("data.table", 
"data.frame"))
ProductCategoryDesc             RegionDesc              SourceDesc                   
Report Apr.2019
1:          CN AMMONIA AB REG 2 UPPER MIDWEST CN-SD, WATERTOWN LIQUID 
InventoryAvailabletoShip        0
2:          CN AMMONIA AB REG 2 UPPER MIDWEST CN-SD, WATERTOWN LIQUID          
NetCashPosition        0
3:          CN AMMONIA AB REG 2 UPPER MIDWEST CN-SD, WATERTOWN LIQUID   
NetMarketPositionTotal        0
4:          CN AMMONIA AB REG 2 UPPER MIDWEST CN-SD, WATERTOWN LIQUID    
NonDirectShipPurchase        0
5:          CN AMMONIA AB REG 2 UPPER MIDWEST CN-SD, WATERTOWN LIQUID         
TotalDirectShips        0
6:          CN AMMONIA AB REG 2 UPPER MIDWEST CN-SD, WATERTOWN LIQUID      
TotalNonDirectShips        0
Current Feb.2019 Jun.2019 Mar.2019 May.2019
1:       0        0        0        0        0
2:       0        0        0        0        0
3:       0      240        0        0        0
4:       0      240        0        0        0
5:       0        0        0        0        0
6:       0      240        0        0        0

标签: rdataframe

解决方案


我们可以尽可能转换为日期,并对列进行排序:

x <- c("ProductCategoryDesc", "RegionDesc","SourceDesc","Report",             
 "Apr.2019","Current","Feb.2019", "Jun.2019",           
 "Mar.2019","May.2019","Mar.2020")

dates <-  as.Date(paste0("01.",x), "%d.%b.%Y")
x <- x[order(replace(dates, is.na(dates), "0000-01-01"))]
# [1] "ProductCategoryDesc" "RegionDesc"          "SourceDesc"          "Report"             
# [5] "Current"             "Feb.2019"            "Mar.2019"            "Apr.2019"           
# [9] "May.2019"            "Jun.2019"            "Mar.2020"         

您的排序数据框:

df[x]

推荐阅读