0

I have a data.frame with two columns $P1 and $P2, both numeric. I'd like to run the power.prop.test in library(stats),by row, and have a third column created that prints only the $n value from the function results. The test should be conducted with power=0.80 in the arguments. The data looks as follows:

structure(list(P1 = c(0.02, 0.02, 0.02, 0.02, 0.02, 0.02), P2 = c(0.022, 
0.024, 0.026, 0.028, 0.03, 0.032)), .Names = c("P1", "P2"), row.names = c(NA, 
6L), class = "data.frame")

There would be a third column, say, $N, where the first element would be 80681.38, the second would be 21108.38, and so forth.

Also, should note that same number repeats in $P1 because I head() the original data.frame that had rep(seq()) of several numbers.

Can some assist? Cheers!

5
  • I've tried apply(tdata, 1, power.prop.test(p1=data$P1, p2=data$P2, power=0.80)$n and N<-function (p1, p2, power) { outcome<-power.prop.test(p1, p2, power) return(outcome) } Commented Apr 6, 2018 at 17:24
  • apply is a bad idea (see here for why); purrr::pmap or the like is a better option. broom::tidy is very helpful to turn the test results into a nice data frame. All together: purrr::pmap_dfr(df, ~broom::tidy(power.prop.test(p1 = .x, p2 = .y, power = 0.8))), or to just add an n column, df$n <- pmap_dbl(df, ~power.prop.test(p1 = .x, p2 = .y, power = 0.8)$n) Commented Apr 6, 2018 at 17:25
  • or in all base R, df$n <- mapply(function(P1, P2){power.prop.test(p1 = P1, p2 = P2, power = 0.8)$n}, df$P1, df$P2) Commented Apr 6, 2018 at 17:34
  • That last one worked perfectly! Apologies, I'm still a novice at this: when I run the third line, I get an error that it could not find function pmap_dbl(). What package is that in? I installed purrr, broom, and tidyverse. Commented Apr 6, 2018 at 17:39
  • pmap (and variants) is from purrr, which you can load explicitly or via library(tidyverse). broom is installed by tidyverse, but not loaded by it. Commented Apr 6, 2018 at 17:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.