-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
function for calculating scales #126
Comments
@genevamarshall, @yutiantang and others are using |
I've been working on something that meets all these requirements except for for the nonuniform weights. row_sum <- function(
d,
columns_to_average = character(0),
pattern,
new_column_name = "row_sum",
threshold_proportion = .75,
verbose = FALSE
) {
if (length(columns_to_average) == 0L) {
columns_to_average <-
d |>
colnames() |>
grep(
x = _,
pattern = pattern,
value = TRUE,
perl = TRUE
)
if (verbose) {
message(
"The following columns will be summed:\n- ",
paste(columns_to_average, collapse = "\n- ")
)
}
}
d |>
dplyr::mutate(
row_sum = # Finding the sum (used by m4)
rowSums(
dplyr::across(!!columns_to_average),
na.rm = TRUE
),
nonmissing_count =
rowSums(
dplyr::across(
!!columns_to_average,
.fns = \(x) { !is.na(x) }
)
),
nonmissing_proportion = nonmissing_count / length(columns_to_average),
{{new_column_name}} :=
dplyr::if_else(
threshold_proportion <= nonmissing_proportion,
row_sum,
# row_sum / nonmissing_count,
NA_real_
)
) |>
dplyr::select(
-row_sum,
-nonmissing_count,
-nonmissing_proportion,
)
# Alternatively, return just the new columns
# dplyr::pull({{new_column_name}})
} |
I'm not sure why it was producing errors before ref #126
@wibeasley Feature request and questions: |
inputs:
The text was updated successfully, but these errors were encountered: