如何重塑数据库

问题描述 投票:0回答:1

如何将A型组织的数据库转变为B型组织?

水果 标记 conf_1 conf_2
橙色 C 123 456
橙色 B 123 456
橙色 A 123 456
苹果 C 987 654
苹果 B 987 654
苹果 A 987 654
李子 C 321 654
李子 B 321 654
李子 A 321 654
梨子 C 765 890
梨子 B 765 890
梨子 A 765 890
葡萄 C 235 652
桃子 A 876 325
桃子 C 876 325

我在 Excel 中有这个数据集,但也在 R 中工作。所以我可以在 Excel 或 R 中执行此操作。但是如何执行呢?我承认这超出了我的能力范围。

excel transformation data-wrangling
1个回答
0
投票

这个 PowerQuery 可以做到:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ChangeType = Table.TransformColumnTypes(Source,{{"fruits", type text}, {"marker", type text}, {"conf_1", Int64.Type}, {"conf_2", Int64.Type}}),
    RemoveDuplicate = Table.Distinct(ChangeType, {"fruits", "conf_1", "conf_2"}),

    PivotColumn = Table.Pivot(ChangeType, List.Distinct(ChangeType[marker]), "marker", "fruits", List.Count),
    MergeData = Table.NestedJoin(PivotColumn, {"conf_1", "conf_2"}, RemoveDuplicate, {"conf_1", "conf_2"}, "Fruits", JoinKind.LeftOuter),
    ExpandData = Table.ExpandTableColumn(MergeData, "Fruits", {"fruits"}, {"fruits"}),
    ReorderColumns = Table.ReorderColumns(ExpandData,{"fruits", "A", "B", "C", "conf_1", "conf_2"})
in
    ReorderColumns  

© www.soinside.com 2019 - 2024. All rights reserved.