Let's say I have a data frame composed of "year" and "cognitive impairment" (1=yes, 0 = otherwise)
I want to compare the proportions each year. Thus, 2000 will be:
df %>%
filter(year == 2000) %>%
{prop.test(rev(table(.$cogimp)),p = 0.5, conf.level=0.95)}
And I can check by:
prop.test(x = 3, n = 30, p = 0.5, conf.level=0.95)
However, it seems to me that I can make these analyses simpler by using broom or purrr. My goal is to have a table like this one:
Code is below:
df <- structure(list(year = c(2000, 2000, 2015, 2015, 2000, 2015, 2000,
2000, 2000, 2000, 2015, 2006, 2015, 2015, 2010, 2006, 2006, 2010,
2000, 2006, 2015, 2006, 2015, 2015, 2000, 2015, 2000, 2015, 2015,
2010, 2015, 2015, 2015, 2000, 2006, 2006, 2006, 2015, 2015, 2006,
2015, 2010, 2000, 2000, 2010, 2006, 2010, 2010, 2015, 2000, 2015,
2006, 2000, 2006, 2015, 2006, 2000, 2010, 2010, 2010, 2015, 2006,
2015, 2000, 2015, 2010, 2010, 2010, 2010, 2000, 2000, 2000, 2006,
2015, 2015, 2000, 2000, 2000, 2015, 2006, 2006, 2010, 2006, 2000,
2010, 2000, 2015, 2015, 2015, 2015, 2010, 2000, 2000, 2010, 2006,
2010, 2010, 2000, 2000, 2000), cogimp = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1,
1, 1, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -100L), class = c("tbl_df",
"tbl", "data.frame"))
df %>%
count(year, cogimp)
df %>%
filter(year == 2006) %>%
{prop.test(rev(table(.$cogimp)),p = 0.5, conf.level=0.95)}
prop.test(x = 3, n = 30, p = 0.5, conf.level=0.95)
prop.test(x = 2, n = 19, p = 0.5, conf.level=0.95)