Re-Order Summaries Of Hierarchical Data In R

27 Views Asked by At

I am working with R and dplyr. I'd like to re-order my data summary according to the hierarchical (geographic) structure given in two columns of my data set (R1 & R2). I have summary values for both each level in R1 (big regions) and R2 (cities) and have them displayed like this:

REGION                     LEVEL                      SUMMARY_VALUE     
<chr>                      <chr>                      <chr>     
R1                          Europe                     x 
R1                          Americas                   x 
R1                          Asia                       x
R2                          Germany                    x 
R2                          Canada                     x
R2                          Japan                      x 
R2                          Italy                      x
R2                          US                         x

SUMMARY_VALUE are some summary statistics calculated using data that looks like this:

R1            R2        VALUE
<chr>         <chr>     <dbl>
Europe        Germany    x
Europe        Germany    x
Europe        Italy      x
Europe        Italy      x
Americas      Canada     x
Americas      Canada     x
Americas      US         x
Americas      US         x
Asia          Japan      x
Asia          Japan      x
.
.
... and so on...

In the end, I'd just like to re-arrange (or map) my summary output above to look like below. Maybe with dplyr?

REGION                     LEVEL                      SUMMARY_VALUE     
<chr>                      <chr>                      <chr>     
R1                          Europe                     x 
R2                          Germany                    x 
R2                          Italy                      x
R1                          Americas                   x 
R2                          Canada                     x
R2                          US                         x
R1                          Asia                       x
R2                          Japan                      x 

Help much appreciated!

0

There are 0 best solutions below