This function adds a new column to the sample_info slot of a mass_dataset object, which contains the frequency of NA (Not Available) values for each sample according to the variables specified.

mutate_sample_na_freq(object, according_to_variables = "all")

Arguments

object

A mass_dataset object.

according_to_variables

A character vector specifying the variable IDs to consider when calculating the frequency of NA values. Default is "all", which considers all variables.

Value

A modified mass_dataset object with an updated sample_info slot.

Author

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
#> -------------------- 
#> massdataset version: 1.0.33 
#> -------------------- 
#> 1.expression_data:[ 1000 x 8 data.frame]
#> 2.sample_info:[ 8 x 4 data.frame]
#> 8 samples:Blank_3 Blank_4 QC_1 ... PS4P3 PS4P4
#> 3.variable_info:[ 1000 x 3 data.frame]
#> 1000 variables:M136T55_2_POS M79T35_POS M307T548_POS ... M232T937_POS M301T277_POS
#> 4.sample_info_note:[ 4 x 2 data.frame]
#> 5.variable_info_note:[ 3 x 2 data.frame]
#> 6.ms2_data:[ 0 variables x 0 MS2 spectra]
#> -------------------- 
#> Processing information
#> 1 processings in total
#> create_mass_dataset ---------- 
#>       Package         Function.used                Time
#> 1 massdataset create_mass_dataset() 2024-09-06 08:49:54

##calculate NA frequency according to all the variables
object2 =
  mutate_sample_na_freq(object = object)

head(extract_sample_info(object))
#>   sample_id injection.order   class   group
#> 1   Blank_3               1   Blank   Blank
#> 2   Blank_4               2   Blank   Blank
#> 3      QC_1               3      QC      QC
#> 4      QC_2               4      QC      QC
#> 5     PS4P1               5 Subject Subject
#> 6     PS4P2               6 Subject Subject
head(extract_sample_info(object2))
#>   sample_id injection.order   class   group na_freq
#> 1   Blank_3               1   Blank   Blank   0.682
#> 2   Blank_4               2   Blank   Blank   0.702
#> 3      QC_1               3      QC      QC   0.397
#> 4      QC_2               4      QC      QC   0.381
#> 5     PS4P1               5 Subject Subject   0.424
#> 6     PS4P2               6 Subject Subject   0.427

##calculate NA frequency according to only variables with mz > 100
variable_id =
object2 %>%
  activate_mass_dataset(what = "variable_info") %>%
  filter(mz > 100) %>%
  pull(variable_id)

object3 =
  mutate_sample_na_freq(object = object2,
                according_to_variables = variable_id)

object3
#> -------------------- 
#> massdataset version: 1.0.33 
#> -------------------- 
#> 1.expression_data:[ 1000 x 8 data.frame]
#> 2.sample_info:[ 8 x 6 data.frame]
#> 8 samples:Blank_3 Blank_4 QC_1 ... PS4P3 PS4P4
#> 3.variable_info:[ 1000 x 3 data.frame]
#> 1000 variables:M136T55_2_POS M79T35_POS M307T548_POS ... M232T937_POS M301T277_POS
#> 4.sample_info_note:[ 6 x 2 data.frame]
#> 5.variable_info_note:[ 3 x 2 data.frame]
#> 6.ms2_data:[ 0 variables x 0 MS2 spectra]
#> -------------------- 
#> Processing information
#> 2 processings in total
#> create_mass_dataset ---------- 
#>       Package         Function.used                Time
#> 1 massdataset create_mass_dataset() 2024-09-06 08:49:54
#> mutate_sample_na_freq ---------- 
#>       Package           Function.used                       Time
#> 1 massdataset mutate_sample_na_freq() 2024-09-06 08:49:54.026715
#> 2 massdataset mutate_sample_na_freq() 2024-09-06 08:49:54.030526

head(extract_sample_info(object3))
#>   sample_id injection.order   class   group na_freq na_freq.1
#> 1   Blank_3               1   Blank   Blank   0.682 0.6834016
#> 2   Blank_4               2   Blank   Blank   0.702 0.7038934
#> 3      QC_1               3      QC      QC   0.397 0.3944672
#> 4      QC_2               4      QC      QC   0.381 0.3811475
#> 5     PS4P1               5 Subject Subject   0.424 0.4211066
#> 6     PS4P2               6 Subject Subject   0.427 0.4241803