mgh
mgh

Reputation: 71

Using chi-squares by multiple groups in R

Here's my dataset:

enter image description here

Can anyone show me how to get the p-value per customer?

Expected output: p-value for AAA, BBB, CCC, DDD, EEE

Thanks!

Upvotes: 0

Views: 436

Answers (1)

Duck
Duck

Reputation: 39585

Try playing around this:

library(dplyr)
#Code
df %>% 
  group_by(Customer) %>% 
  summarise(pval = chisq.test(Age.Group, case)$p.value)

Output:

# A tibble: 2 x 2
  Customer  pval
  <chr>    <dbl>
1 AAA      0.213
2 BBB      0.213

Some data used:

#Data
df <- structure(list(Age.Group = c("18-34", "35-44", "45-54", "55-64", 
"18-34", "35-44", "45-54", "55-64"), Customer = c("AAA", "AAA", 
"AAA", "AAA", "BBB", "BBB", "BBB", "BBB"), case = c(82L, 100L, 
200L, 12L, 92L, 110L, 210L, 22L), control = c(100L, 45L, 23L, 
9L, 95L, 40L, 18L, 4L)), class = "data.frame", row.names = c(NA, 
-8L))

Upvotes: 2

Related Questions