[R] Setting up hypothesis tests with the infer library?
Neal Fultz
n|u|tz @end|ng |rom gm@||@com
Sat Mar 29 21:46:44 CET 2025
>
> I've been setting up problems like this with code similar to:
> ===========================
> df <- data.frame(
> survey = c(rep("1980", 1000), rep("2010", 1000)),
> DP = c(rep("Y", 0.66*1000), rep("N", 1000 - (0.66*1000)),
> rep("Y", 0.64*1000), rep("N", 1000 - (0.64*1000))))
>
I think infer needs the data in 'long' format. Here is how I would approach
it:
df <- expand.grid(year=c(1980, 2010), DP=c("Approve", "Disapprove"))
expand.grid creates all combos of year and view
df <- df[ rep(1:4, c(660, 640, 1000-660, 1000-640)), ]
here I used dataframe indexing to repeat each of the four rows. I find this
a lot easier than manually creating columns the way you did.
tidyverse-style packages don't encourage this style though. Tastes will
vary.
In dplyr style, add a column and use it in slice:
df <- expand.grid(year=c(1980, 2010), DP=c("Approve", "Disapprove")) %>%
mutate(freq = c(660, 640, 1000-660, 1000-640)) %>%
slice(rep(1:n(), freq))
xtabs(~year+DP, df)
checks that it worked correctly. Or use dplyr::count() if that'swhat you're
comfortable with.
[[alternative HTML version deleted]]
More information about the R-help
mailing list