Home > Blockchain >  Reshaping columns to rows and have apercentage
Reshaping columns to rows and have apercentage

Time:08-19

I have a data like this:

structure(list(A = c("a", "b", "c", "c", "c", "b", "a", "b"), 
    B = c("b", "b", "c", "a", "b", "c", "c", "a"), C = c("c", 
    "c", "c", "a", "a", "a", "b", "b"), D = c("a", "b", "c", 
    "c", "c", "a", "b", "b"), group = c("x", "y", "x", "x", "x", 
    "y", "y", "y")), class = "data.frame", row.names = c(NA, 
-8L))

I want to reshape it that columns' name shift to rows, and also have a percentage (per column) of stack in each group based on facet name.

The desire data is like this (per column is not accurate) :

   facet group stack  per
1      A     x     a  2.1
2      A     y     b  4.2
3      A     x     c 10.2
4      A     y     a 20.2
5      A     x     b  5.6
6      A     y     c 11.7
7      B     x     a  5.4
8      B     y     b 17.7
9      B     x     c  9.0
10     B     y     a 14.7
11     B     x     b  3.2
12     B     y     c 13.5
13     C     x     a  8.8
14     C     y     b 11.5
15     C     x     c  0.7
16     C     y     a  7.3
17     C     x     b  6.8
18     C     y     c  5.4
19     D     x     a  7.9
20     D     y     b 12.2
21     D     x     c 16.1

CodePudding user response:

Perhaps something like this using dplyr and tidyr? First pivot_longer, then get group/facet/stack counts, and divide those counts by the sum of such counts (within group/facet).

library(dplyr)
library(tidyr)

pivot_longer(data, -group, names_to="facet", values_to = "stack") %>% 
  group_by(facet,group,stack) %>% 
  summarize(per =n()) %>% 
  mutate(per =per/sum(per))

Output:

   facet group stack   per
   <chr> <chr> <chr> <dbl>
 1 A     x     a      0.25
 2 A     x     c      0.75
 3 A     y     a      0.25
 4 A     y     b      0.75
 5 B     x     a      0.25
 6 B     x     b      0.5 
 7 B     x     c      0.25
 8 B     y     a      0.25
 9 B     y     b      0.25
10 B     y     c      0.5 
11 C     x     a      0.5 
12 C     x     c      0.5 
13 C     y     a      0.25
14 C     y     b      0.5 
15 C     y     c      0.25
16 D     x     a      0.25
17 D     x     c      0.75
18 D     y     a      0.25
19 D     y     b      0.75
  • Related