0

I'm running a number of power analyses, testing different observation sizes keeping all else equal, and would like to save the results as rows in a dataframe so that I can visualise them later in ggplot.

results_df <- data.frame()

# create function leaving user to control n= as an input parameter

run_power_analysis <- function(n){
  
  power_analysis <- pwr.t.test(n=n,
                            sig.level=0.05,
                            power=NULL,
                            d=0.2,
                            type="one.sample",
                            alternative = "two.sided")
  
  # save $n and $power as a row in df

  results_df <- rbind(results_df, power_analysis[c(1,4)])
  return(results_df)
  }

But this doesn't seem to update results_df. R returns only 1 row, why is this? And how can I get the whole df?

  • From the online doc, `pwr.t.test` returns a list, not a vector, so you may need `power_analysis[[c(1,4)]]`. or a named list: `power_analysis[[c("n", "power")]]`. – Limey Feb 18 '22 at 15:49
  • Also consider that the `powder` package may have done most of your work for you. See [here](https://stackoverflow.com/questions/71176195/saving-power-analysis-pwr-package-results-to-a-df). – Limey Feb 18 '22 at 15:50
  • I don’t see any sapply or for-loop. – IRTFM Feb 19 '22 at 05:04

0 Answers0