0

I have a data frame that has "Value", followed by Decile_Category_1, Decile_Category_2,..., Decile_Category_n.

I want to make n separate df's grouped by their decile and then want to make a graph on each pivot table. What's a quick way to do that other than typing out the group_by n times?

Edit: Example Starting DF:

Value  Dec_Cat_1    Dec_Cat_2   Dec_Cat_3
100    1            3             1      
200    3            3             3
400    1            6             3 
...    ...          ...          ...

Ending DF1

Dec_Cat_1   Value
1           500
2           xxx
3           200
4           xxx
5           xxx          
6           xxx
7           xxx
8           xxx
9           xxx
10          xxx

Ending DF2

Dec_Cat_2   Value
1           xxx
2           xxx
3           300
4           xxx
5           xxx          
6           400
7           xxx
8           xxx
9           xxx
10          xxx

And so on for every column in the original data frame.

user3304359
  • 335
  • 1
  • 9
  • See if this helps: [Reshaping data.frame from wide to long format](https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format). – Rui Barradas Dec 04 '18 at 20:02
  • 2
    Could you include an example dataframe, and a example of the output you want? – Lamia Dec 04 '18 at 20:02
  • @Lamia, I added an example on how the df looks and how I want it to end up – user3304359 Dec 04 '18 at 20:23
  • Please consider [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – markus Dec 04 '18 at 20:41

1 Answers1

0
## Dataset
df = read.table(text="Value  Dec_Cat_1    Dec_Cat_2   Dec_Cat_3
100    1            3             1      
200    3            3             3
400    1            6             3", h=T)

If you're dealing with multiple dataframes, you may want to put them in a list. First, seperate your dataframe of n columns into a list containing n-1 dataframes:

l1 = lapply(df[-1],function(x) cbind(df[1],x))
$Dec_Cat_1
  Value x
1   100 1
2   200 3
3   400 1

$Dec_Cat_2
  Value x
1   100 3
2   200 3
3   400 6

$Dec_Cat_3
  Value x
1   100 1
2   200 3
3   400 3

Then, using functions from dplyr and tidyr, you can summarise your value:

library(dplyr)
library(tidyr)
res = lapply(l1,function(d) d %>% group_by(x) %>% summarise(Value = sum(Value)) %>% complete(x = 1:10))
> res
$Dec_Cat_1
# A tibble: 10 x 2
       x Value
   <int> <int>
 1     1   500
 2     2    NA
 3     3   200
 4     4    NA
 5     5    NA
 6     6    NA
 7     7    NA
 8     8    NA
 9     9    NA
10    10    NA

$Dec_Cat_2
# A tibble: 10 x 2
       x Value
   <int> <int>
 1     1    NA
 2     2    NA
 3     3   300
 4     4    NA
 5     5    NA
 6     6   400
 7     7    NA
 8     8    NA
 9     9    NA
10    10    NA

$Dec_Cat_3
# A tibble: 10 x 2
       x Value
   <int> <int>
 1     1   100
 2     2    NA
 3     3   600
 4     4    NA
 5     5    NA
 6     6    NA
 7     7    NA
 8     8    NA
 9     9    NA
10    10    NA
Lamia
  • 3,845
  • 1
  • 12
  • 19