mutate_mean_intensity: Add Mean Intensity to mass_dataset Object

View source: R/mutate_mean_intensity.R

mutate_mean_intensityR Documentation

Add Mean Intensity to mass_dataset Object

Description

This function calculates the mean intensity for each variable in the mass_dataset object based on the specified samples and adds it as a new column to the variable information.

Usage

mutate_mean_intensity(object, according_to_samples = "all", na.rm = TRUE)

Arguments

object

A mass_dataset object.

according_to_samples

A character vector specifying the samples to consider for the mean calculation. Default is "all".

na.rm

Logical, whether to remove NA values before calculating the mean Default is TRUE.

Value

A modified mass_dataset object with added mean intensity information.

Author(s)

Xiaotao Shen shenxt1990@outlook.com

Examples

data("expression_data")
data("sample_info")
data("variable_info")

object =
  create_mass_dataset(
    expression_data = expression_data,
    sample_info = sample_info,
    variable_info = variable_info,
  )

object

##calculate mean intensity according to all the samples
object2 =
  mutate_mean_intensity(object = object, na.rm = TRUE)

object2

head(extract_variable_info(object))
head(extract_variable_info(object2))

##calculate mean intensity according to only QC samples
object3 =
  mutate_mean_intensity(object = object2,
                according_to_samples =
              get_sample_id(object)[extract_sample_info(object)$class == "QC"])

object3

head(extract_variable_info(object3))
###remain variables with mean intensity (QC) /  mean intensity (Blank) > 3
qc_sample_name =
  get_sample_id(object)[extract_sample_info(object)$class == "QC"]
blank_sample_name =
  get_sample_id(object)[extract_sample_info(object)$class == "Blank"]

object4 =
object %>%
  mutate_mean_intensity(according_to_samples = qc_sample_name,
                        na.rm = TRUE) %>%
  mutate_mean_intensity(according_to_samples = blank_sample_name,
                        na.rm = TRUE) %>%
  activate_mass_dataset(what = "variable_info") %>%
  mutate(mean_intensity.1 = case_when(
    is.na(mean_intensity.1) ~ 0,
    TRUE ~ mean_intensity.1
  )) %>%
  mutate(mean_intensity = case_when(
    is.na(mean_intensity) ~ 0,
    TRUE ~ mean_intensity
  )) %>%
  mutate(qc_blank_ratio = mean_intensity.1 / mean_intensity) %>%
  mutate(qc_blank_ratio = case_when(
    is.na(qc_blank_ratio) ~ 0,
    TRUE ~ qc_blank_ratio
  )) %>%
  filter(qc_blank_ratio > 3)

object4
object4 %>%
  extract_variable_info()

tidymass/massdataset documentation built on Sept. 14, 2024, 4:41 p.m.