r - 重新排序名称包含日期顺序的数据框列?
问题描述
我有一个反应性数据框,其中列名更改并且名称列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
解决方案
我们可以尽可能转换为日期,并对列进行排序:
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]
推荐阅读
- macros - Doyxgen一次性宏扩展/扩展命令
- jenkins - 尝试使用 Homebrew 安装 jenkins 时出错
- azure-blob-storage - Azure Blob 存储不在本地存储
- c++ - C++ 使用 OpenMP #pragma 和 std:thread?
- python - 如何从 PANDASQL 中的特定日期前进/后退,例如 oracle date-1 和 impala DAYS_ADD(date,-1)
- docker - Docker 通过 Gradle,不起作用,DockerCompose 可以
- javascript - 如何根据另一个值在嵌套数组中查找一个值?
- schemacrawler - 删除 schemacrawler 中的“生成”标签
- vue.js - Vue 3 是否向后兼容 Vue 2?
- android - 如何从模块中的项目 build.gradle 访问额外的属性