重新排序名称包含按日期顺序排列的日期的数据框列?

时间:2019-03-21 16:28:56

标签: r dataframe

我有一个反应式数据框,其中列名更改,而名称为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

2 个答案:

答案 0 :(得分:4)

只要有可能,我们都可以转换为日期,并对列进行排序:

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]

答案 1 :(得分:0)

享受!

# Reorder columns in dataframe
df =
  df[
    c("ProductCategoryDesc",
      "RegionDesc", 
      "SourceDesc",
      "Report",
      "Current",
      "Feb.2019",
      "Mar.2019",
      "Jun.2019",
      "Apr.2019",
      "May.2019",
      "Mar.2020")]

要改为通过位置aka索引来更改数据框的顺序,请尝试:

df = 
  df[c(
       1,
       3,
       2)]

如果每次数据添加到数据框时都能预期到该数据框的哪个索引是最新的,则可以编写脚本以使它将采用该数据框并将其移动到所需位置。例如,如果它是数据帧中的最后一列,并且您想开始将其移到第一位置,则可以尝试使用诸如length(df)之类的方法,并使用该长度来指示最后一个元素:

df = 
  df[c(
       length(df),
       1,
       3,
       2)]