I am trying to create a new column based on a calculation that uses input values from other columns for the same row that is being computed. I am using a function based on another package to conduct my calculations. It will error out due to NAs. But the function I am using does not take the na.rm=T argument. The code below first has an example of the function working ( sample_size <- pwr.t.test) based on explicitly provided input values. Once I try to put this into a mutate function in the next attempt it fails.
library(pwr)
library(tidyverse)
mast_reff <- data.frame(
stringsAsFactors = FALSE,
ind = c("PctOverheadCover","PctBankOverheadCover",
"VegComplexity","VegComplexityWoody",
"VegComplexityUnderstoryGround","PctNoxiousWoodySpecies"),
mean_val = c(37.7796407185629,72.4149700598802,1.46291666666667,
0.86375,0.80577380952381,0),
sd_val = c(32.5625783793838,24.7446784561184,0.410474173251117,
0.352326208910194,0.33514050503087,0)
)
## These variables are needed for the sample_size code below and ensures the calculations work
mean <- 38
std_dev <- 32
delta <- 0.20
alpha <- 0.10
power <- 0.8
sample_size <- pwr.t.test(d = (delta * mean / std_dev),
power = power,
sig.level = alpha,
type = "two.sample",
alternative = "two.sided")$n
print(sample_size)
#> [1] 219.8835
## Replace the "mean" and "std_dev" inputs with values from the mast_reff data.frame ("mean_val" and "sd_val")
## and populate a new column "nSize" with the result
mast_reff |>
mutate(nSize = pwr.t.test(d = (delta*mean_val / sd_val),
power = power,
sig.level = alpha,
type = "two.sample",
alternative = "two.sided")$n, .by = ind
)
#> Error in `mutate()`:
#> ℹ In argument: `nSize = `$`(...)`.
#> ℹ In group 6: `ind = "PctNoxiousWoodySpecies"`.
#> Caused by error in `uniroot()`:
#> ! f.lower = f(lower) is NA
Created on 2024-03-05 with reprex v2.1.0
Any feedback is Greatly appreciated!!