Skip to content

Commit

Permalink
Summarise works
Browse files Browse the repository at this point in the history
  • Loading branch information
Klorator committed Feb 8, 2024
1 parent df4fb20 commit fd252cd
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 17 deletions.
99 changes: 92 additions & 7 deletions R/Fic_calculations.R
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,102 @@ ras.Fu_feces_mass_balance_10.2.5 <- function(df_calc,
return(df_calc)
}

ras.Fu_feces_summary <- function(df,
#' Fu feces mean & SD
#'
#' Add mean & SD columns, grouped by `grouping_col`, for all
#' the calculated numeric columns.
#'
#' @param df Data frame
#' @param grouping_col Column name to group by
#' @param sample_type Column name for sample type
#' @param compound Column name for compound
#'
#' @return Data frame with mean & SD columns
#' @export
#'
#' @examples
#' # No example
ras.Fu_feces_meanSD <- function(df,
grouping_col = "Sample_ID",
.summarize = TRUE,
.SD = TRUE) {
if (.SD) {
sample_type = "Sample Type",
compound = "Analyte Peak Name") {
df_sum <- df %>%
dplyr::select(
-tidyselect::any_of(c(
"Date",
"Initials",
"Sample_origin",
"Dosing",
"Timepoint",
"Replicate",
{{sample_type}},
{{compound}}
)
)
)

}
num_columns <- df_sum %>%
dplyr::select(tidyselect::where(is.numeric)) %>%
colnames()

if (.summarize) {
for (col in num_columns) {
col_mean <- paste0(col,"_mean")
col_sd <- paste0(col,"_sd")

df <- df %>%
dplyr::mutate(
{{col_mean}} := mean(.data[[col]], na.rm = T),
{{col_sd}} := sd(.data[[col]], na.rm = T),
.after = {{col}},
.by = {{grouping_col}}
)
}

return(df)
}

ras.Fu_feces_summarize <- function(df,
grouping_col = "Sample_ID",
sample_type = "Sample Type",
compound = "Analyte Peak Name") {
df_sub <- df %>%
dplyr::select(
-tidyselect::any_of(c(
"Date",
"Initials",
"Sample_origin",
"Dosing",
"Timepoint",
"Replicate",
{{sample_type}},
{{compound}}
)
)
)

num_columns <- df_sub %>%
dplyr::select(tidyselect::where(is.numeric)) %>%
colnames()

df_sum <- df %>%
dplyr::select({{grouping_col}}) %>%
unique()

for (col in num_columns) {
col_mean <- paste0(col,"_mean")
col_sd <- paste0(col,"_sd")

df_temp <- df %>%
dplyr::summarise(
{{col_mean}} := mean(.data[[col]], na.rm = T),
{{col_sd}} := sd(.data[[col]], na.rm = T),
.by = {{grouping_col}}
)

df_sum <- dplyr::right_join(
x = df_sum,
y = df_temp,
by = {{grouping_col}}
)
}
return(df_sum)
}
28 changes: 18 additions & 10 deletions R/Fu_feces_workflow.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#' `NULL` => "Analyte Peak Name"
#' @param .checkValues Removes "<" from the values column to make sure it can
#' be coerced to numeric.
#' @param .summarize Summarize values into averages
#' @param .SD Create column with Standard Deviation
#' @param .summarize Summarize the data into unique rows in
#' grouping column. Drops the original value columns.
#'
#' @return Dataframe with all variables, used & calculated.
#' Also writes data frame to .csv (EU) & .xlsx files.
Expand All @@ -42,8 +42,7 @@ ras.Fu_feces_workflow <- function(
MassBalance_2.5 = 1.75,
.compound = "Analyte Peak Name",
.checkValues = TRUE,
.summarize = TRUE,
.SD = TRUE
.summarize = FALSE
) {
# Set output directory
output_dir <- tcltk::tk_choose.dir(caption = "Select output directory")
Expand Down Expand Up @@ -106,12 +105,21 @@ ras.Fu_feces_workflow <- function(
mass_factor = MassBalance_2.5
)

df_calc <- df_calc %>%
ras.Fu_feces_summary(
grouping_col = "Sample_ID",
.summarize = .summarize,
.SD = .SD
)
if (.summarize) {
df_calc <- df_calc %>%
ras.Fu_feces_summarize(
grouping_col = "Sample_ID",
sample_type = Sample_type,
compound = .compound
)
} else {
df_calc <- df_calc %>%
ras.Fu_feces_meanSD(
grouping_col = "Sample_ID",
sample_type = Sample_type,
compound = .compound
)
}


# Write df_calc to file ----
Expand Down

0 comments on commit fd252cd

Please sign in to comment.