0

I struggle with a seemingly simple issue which I could not find an answer to here.

I analyze a data set represented as a data frame with some questionnaire results represented as factors (called A1-A13, approx 100 rows). Each row has some additional information, such as Gender (Factor) and Age (Numerical). An example for this data set would be:

library(palmerpenguins)
penguins
# A tibble: 344 × 8
   species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex     year
   <fct>   <fct>              <dbl>         <dbl>             <int>       <int> <fct>  <int>
 1 Adelie  Torgersen           39.1          18.7               181        3750 male    2007
 2 Adelie  Torgersen           39.5          17.4               186        3800 female  2007
 3 Adelie  Torgersen           40.3          18                 195        3250 female  2007

The columns "species" and "island" are examples for columns I am interested in.

I now perform the following analysis for each columns:

# Histogram
histogram( ~ species , data=penguins, ylim = c(0,100), drop.unused.levels = FALSE)

# Ordinal regression
mod<-polr(**species** ~ sex + yea, data=penguins,Hess=T)

# Summary
summary(mod)

# Get p values
tidy(mod, conf.int = FALSE, conf.level = 0.95, exponentiate = FALSE, p.values = TRUE)

# Draw histogram
histogram( ~ **species** | sex, data=dta, drop.unused.levels = FALSE, layout=c(1,2), ylim=c(0,100))

As output, I want to have the histograms, the output from summary and tidy as well as the second histogram.

For the example data, I would have this done for species and island columns. However, my data has 13 colums I want to repeat this process for. Is there any other elegant way to program this to be repeated for columns A1-A13?

Thank you!

moritz
  • 3
  • 3
  • By repeating, actually what is your intended output? A list with p values? or something else? – AnilGoyal Aug 29 '22 at 10:10
  • Please also post some sample data! or try to replicate your problem on some publicly available data, so that the question is reproducible – AnilGoyal Aug 29 '22 at 10:11
  • Sounds like [Running a model on separate groups](https://drsimonj.svbtle.com/running-a-model-on-separate-groups) could be helpful, but difficult to say without seeing some data. – neilfws Aug 29 '22 at 10:26
  • Does this answer your question? [Loop over columns of dataframe in R](https://stackoverflow.com/questions/60263342/loop-over-columns-of-dataframe-in-r). Note that `summarise_at(...` has been superseded by `summarise(across(...`. – Limey Aug 29 '22 at 10:28
  • Thanks for your suggestions, I included some sample data and more details, I hope my questions is better understandable now. With summarise(across(..)), I do not get the histograms as output. – moritz Aug 29 '22 at 10:59
  • It would help if the example data bore some resemblance to your real data... – neilfws Aug 29 '22 at 11:01

0 Answers0