Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

grouping by category and then find out difference between categories [r]

$
0
0

I am calculating mean employment rate for different groups from year 1995 to 2015. And then calculate the difference of mean employment rates between groups.

This should be yearly ordered.

Most of time, I tried using summarize function in dplyr, but failed.

The code below is what I set up.

diff_in_diff <- Cps_total %>% 
  filter(age >= 19 & age <= 44) %>% 
  mutate(women_and_black_men = ifelse(female == 1 & marstat != 1 & nfchild == 0, "Single without children",
                                 ifelse(female == 1 & marstat != 1 & nfchild > 0, "Single with children",
                                        ifelse(female == 1 & marstat == 1 & nfchild == 0, "Married without children",
                                               ifelse(female == 1 & marstat == 1 & nfchild > 0, "Married with children",
                                                      ifelse(female == 0 & wbhao == 2, "Black Men", "Otherwise Men"))))))


diff_in_diff_2 <- diff_in_diff %>% 
  filter(!is.na(empl)) %>% 
  group_by(year, women_and_black_men) %>% 
  summarize(mean_empl=mean(empl))
year |  women_and_black_men      |      mean_empl

1995 |  Black Men                |      0.8772406       
1995 |  Married with children    |      0.6810999       
1995 |  Married without children |      0.8227718       
1995 |  Otherwise Men            |      0.9048232       
1995 |  Single with children     |      0.8330486       
1995 |  Single without children  |      0.8927759       
1996 |  Black Men                |      0.8415265       
1996 |  Married with children    |      0.6800505       
1996 |  Married without children |      0.8188101       
1996 |  Otherwise Men            |      0.9035344   

This is what I found.

However, I want to find the value of difference between "Single with children minus Black men", "Single with children minus Single without children", "Single with children minus Married with children", "Single with children minus Married without children" and "Single with children minus Otherwise Men"

Therefore my expectation is:

year |  Single_with_children_vs      |      diff_in_diff

1995 |  vs_Married with children     |      0.031230201
1995 |  vs Married without children  |     -0.130002012
1995 |  vs Single_without_children   |     -0.190230201
1995 |  vs Black Men                 |      0.002030210
1996 |
.
.
.

stuff like this.


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>