1
votes

I have difficulties summarising a data.frame that looks like this:

db <- data.frame(ID = c(rep(1, 3), rep(2,4), rep(3, 2), 4),
             Gender = factor(c(rep("woman", 7), rep("man", 2), "woman")),
             Grade = c(rep(3, 3), rep(1, 4), rep(2, 2), 1),
             Drug = c(1, 2, 2, 1, 2, 6, 9, 8, 5, 1),
             Group = c(rep(1, 3), rep(2,4), rep(1, 2), 2))
db

#    ID Gender Grade Drug Group
# 1   1  woman     3    1     1
# 2   1  woman     3    2     1
# 3   1  woman     3    2     1
# 4   2  woman     1    1     2
# 5   2  woman     1    2     2
# 6   2  woman     1    6     2
# 7   2  woman     1    9     2
# 8   3    man     2    8     1
# 9   3    man     2    5     1
# 10  4  woman     1    1     2

Ideally, I would have one row per observation, but because the Drugs vary over time I end-up with a lot of duplicate rows. This makes the analysis difficult for me.

My ultimate goal is to construct a summary table as already discussed in another post: Using dplyr to create summary proportion table with several categorical/factor variables. Something like this:

| Variable | Group 1 | Group 2 | difference Group 1/2 |
| Gender ................................| .........................p = 1 |
| Male..... |...........1 | ............0 | ..................................|
| Female. |...........1 |.............2 |...................................|

However, since this post was only partially answered and is not directly applicable to my problem (mainly due to the duplicate rows), I would already be happy if could perform the summary statistics separately. In this post: How to get the frequency from grouped data with dplyr? I asked how to obtain the unique/distinct frequencies from the observations. Now, I need to find out if there is a statistical significant difference in the distribution of the genders between the two groups.


According to the ID, I know that there are four observations of which three are female and one is male. So the desired outcome could be calculated like this:

gen <- factor(c("woman", "woman", "man", "woman"))
gr <- c(1, 2 ,1 ,2)
chisq.test(gen, gr)

#   Pearson's Chi-squared test with Yates' continuity correction
# 
# data:  gen and gr
# X-squared = 0, df = 1, p-value = 1
#
# Warning message:
# In chisq.test(gen, gr) : Chi-squared approximation may be incorrect

How can I calculate the p-vale from my data.frame with the use of dplyr?


My failing approach was:

db %>% 
  group_by(ID) %>% 
  distinct(ID, Gender, Group) %>% 
  summarise_all(funs(chisq.test(db$Gender, 
                               db$Group)$p.value))
# A tibble: 4 x 3
#      ID Gender Group
#  <dbl>  <dbl> <dbl>
# 1    1.  0.429 0.429
# 2    2.  0.429 0.429
# 3    3.  0.429 0.429
# 4    4.  0.429 0.429
# Warning messages:
# 1: In chisq.test(db$Gender, db$Group) :
#   Chi-squared approximation may be incorrect
# 2: In chisq.test(db$Gender, db$Group) :
#   Chi-squared approximation may be incorrect
# 3: In chisq.test(db$Gender, db$Group) :
#  Chi-squared approximation may be incorrect
# 4: In chisq.test(db$Gender, db$Group) :
#  Chi-squared approximation may be incorrect
# 5: In chisq.test(db$Gender, db$Group) :
#   Chi-squared approximation may be incorrect
# 6: In chisq.test(db$Gender, db$Group) :
#  Chi-squared approximation may be incorrect
# 7: In chisq.test(db$Gender, db$Group) :
#  Chi-squared approximation may be incorrect
# 8: In chisq.test(db$Gender, db$Group) :
#  Chi-squared approximation may be incorrect
1

1 Answers

1
votes

We can ungroup and then get the pvalue with summarise

db %>% 
  group_by(ID) %>% 
  distinct(ID, Gender, Group) %>%
  ungroup %>%
  summarise(pval = chisq.test(Gender, Group)$p.value)