#### Custom Function Using Stringr Package and Curly Brackets

I am performing a systematic review and have a mix of studies that contain both mean (SD) and median (IQR). I’m trying to create a function to convert median (IQR) to mean (SD) using the Wan method (2014). I have labelled the median data with ‘number’m (eg `34m`), and the IQR is a range (eg `16-45`).

Here is a dataset:

``````library(tidyverse)

data <- tibble(
study = c(1,2,3),
age_median = c(52m, 42m, 69m),
age_iqr = c("18-74", "38-55", "60-91")
number = c(65,30,45)
)
``````

I’m able to calculate both the mean and the sd from the median & IQR (using the Wan method) with the following syntax:

``````# Calculate Age Mean from Median
data <- data |>
separate_wider_delim(age_median, "m", names = c("age_median", NA)) |>
separate_wider_delim(age_iqr, "-", names = c("age_iqr_low", "age_iqr_high"), too_few = "debug") |>
mutate_at(c("age_median", "age_iqr_low", "age_iqr_high"), as.numeric) |>
mutate(age_mean = (age_median + age_iqr_low + age_iqr_high)/3)

# Calculate Age SD from IQR
data <- data |>
mutate(age_sd = (age_iqr_high- age_iqr_low)/(2*qnorm((0.75*number - 0.125)/( number+ 0.25), 0,1)))

``````

I’m trying to wrap this into a function for additional intervention arms:

``````# Function to convert median into mean
med_to_mean <- function(median_col, iqr_col){
data |>
separate_wider_delim({{median_col}}, "m", names = c("median_col", NA)) |>
separate_wider_delim({{iqr_col}}, "-", names = c("iqr_col_low", "iqr_col_high"), too_few = "debug") |>
mutate_at(c("{{median_col}}", "iqr_col_low", "iqr_col_high"), as.numeric) |>
mutate({{median_col}} = (median_col + iqr_col_low + iqr_col_high)/3)

return({{median_col}})

# Calculate Age SD from IQR
data <- data |>
mutate({{IQR_col}} = (iqr_col_high- iqr_col_low)/(2*qnorm((0.75*number - 0.125)/( number+ 0.25), 0,1)))

return({{iqr_col}})
}
``````

When I try to use the function:

``````data <- data |> med_to_mean(age_median, age_iqr)
``````

I keep getting the error:

``````Error in med_to_mean(age_median, age_iqr) :
unused argument (age_iqr)

``````

I know it’s probably something simple, but your help would be greatly appreciated.

Cheers,
Ben

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website