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!
apply(tdata, 1, power.prop.test(p1=data$P1, p2=data$P2, power=0.80)$nandN<-function (p1, p2, power) { outcome<-power.prop.test(p1, p2, power) return(outcome) }applyis a bad idea (see here for why);purrr::pmapor the like is a better option.broom::tidyis 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 anncolumn,df$n <- pmap_dbl(df, ~power.prop.test(p1 = .x, p2 = .y, power = 0.8)$n)df$n <- mapply(function(P1, P2){power.prop.test(p1 = P1, p2 = P2, power = 0.8)$n}, df$P1, df$P2)pmap_dbl(). What package is that in? I installedpurrr,broom, andtidyverse.pmap(and variants) is from purrr, which you can load explicitly or vialibrary(tidyverse). broom is installed by tidyverse, but not loaded by it.