Home > Back-end >  for loop in r -looping through vector
for loop in r -looping through vector

Time:10-13

I am trying to loop through all the cols in my df and run a prop test on each of them.

library(gss)

To run on just one variable I can use--

infer::prop_test(gss,
          college ~ sex,
          order = c("female", "male"))

But now I want to run this for each variable in my df like this:

cols <- gss %>% select(-sex) %>% names(.)

for (i in cols){
  # print(i)
  prop_test(gss,
            i~sex)
}

But this loop does not recognize the i;

Error: The response variable `i` cannot be found in this dataframe.

Any suggestions please??

CodePudding user response:

We need to create the formula. Either use reformulate

library(gss)
library(infer)

out <- vector('list', length(cols))
names(out) <- cols
for(i in cols) {
    out[[i]] <- prop_test(gss, reformulate("sex", response = i))
}

-output

> out
$college
# A tibble: 1 × 6
  statistic chisq_df p_value alternative lower_ci upper_ci
      <dbl>    <dbl>   <dbl> <chr>          <dbl>    <dbl>
1 0.0000204        1   0.996 two.sided    -0.0917    0.101

$partyid
# A tibble: 1 × 3
  statistic chisq_df p_value
      <dbl>    <dbl>   <dbl>
1      12.9        3 0.00484

$class
# A tibble: 1 × 3
  statistic chisq_df p_value
      <dbl>    <dbl>   <dbl>
1      2.54        3   0.467

$finrela
# A tibble: 1 × 3
  statistic chisq_df p_value
      <dbl>    <dbl>   <dbl>
1      9.11        5   0.105

or paste

for(i in cols) {
    prop_test(gss, as.formula(paste0(i, " ~ sex")))
}

data

library(dplyr)
data(gss)
cols <- gss %>% 
    select(where(is.factor), -sex, -income) %>% 
    names(.)
  • Related