Skip to content

Commit

Permalink
refactor code to simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
andersone1 committed Nov 21, 2024
1 parent 0807689 commit ffac147
Showing 1 changed file with 7 additions and 23 deletions.
30 changes: 7 additions & 23 deletions R/identify-subject-cols.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,20 @@ identify_subject_cols <- function(.df, .subject_col) {
stop("Subject column '", .subject_col, "' does not exist in the data frame.")
}

# Summarize within subjects to check if columns are constant in each subject
subject_columns <-
# Identify columns that are constant within all subjects
constant_cols <-
.df %>%
dplyr::group_by(!!rlang::sym(.subject_col)) %>%
# Summarize each column by checking if all values within each subject are identical
dplyr::summarise(
dplyr::across(
tidyselect::everything(),
~ dplyr::n_distinct(.x) == 1
)
dplyr::across(tidyselect::everything(), ~ dplyr::n_distinct(.x) == 1)
) %>%
dplyr::ungroup()

# Identify columns that are constant within all subjects
constant_cols <-
subject_columns %>%
# For each column (excluding the subject column), check if it is constant across all subjects
dplyr::ungroup() %>%
# Summarize all columns other than .subject_col
dplyr::summarise(
dplyr::across(
-!!rlang::sym(.subject_col),
~ all(.x)
)
) %>%
# Transform the data into a long format with column names and their corresponding boolean values
tidyr::pivot_longer(
cols = tidyselect::everything()
across(-!!rlang::sym(.subject_col), ~ all(.x))
) %>%
# Filter to retain only those columns that are constant within each subject across the entire data frame
tidyr::pivot_longer(cols = tidyselect::everything()) %>%
dplyr::filter(value) %>%
# Extract the names of these columns
dplyr::pull(name) %>%
sort()

Expand Down

0 comments on commit ffac147

Please sign in to comment.