-
Notifications
You must be signed in to change notification settings - Fork 0
/
subset_data.qmd
161 lines (145 loc) · 4.43 KB
/
subset_data.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Subset data
```{r}
#| results: "asis"
#| echo: false
source("_common.R")
```
## Introduction
In this chapter we will look at how to subset data
### Prerequisites
```{r}
#| label: setup
#| message: false
library(here)
library(tidyverse)
library(SLINGtools)
```
## Select and rename columns
```{r select-rename-columns}
#| results: markup
# d_subset <- d_orig |> dplyr::select(`AnalysisID` = "DataFileName",
# AcqTimeStamp, Compound = Feature,
# Area = Intensity)
# print(d_subset)
# ```
#
# ## Filter table for specific text
#
# ```{r filter-tables}
#
# d_subset |> filter(AnalysisID == "149_BQC_PQC17.d ")
#
# d_subset |> filter(AnalysisID == "149-bQC_PQC17.d",Compound == "TG 48:1 [-18:1]")
#
# "CE 18:1" == "CE 18:1"
# "CE 18:1" == "CE 18:1 "
# "CE 18:1" == "CE 18:1"
# "Ce 18:1" == "CE 18:1"
#
# stringr::str_trim("CE 18:1 ")
# stringr::str_trim(" CE 18:1 ")
# stringr::str_squish(" CE 18:1 ")
#
# stringr::str_detect("LPC 18:1 (IS)", pattern = "IS")
# stringr::str_detect("LPC 18:1 (ISTD)", pattern = "IS")
# stringr::str_detect("LPC 18:1 (IS)", pattern = "ISTD")
# stringr::str_detect("LPC 18:1 (IS)", pattern = "LPC")
#
# stringr::str_replace("Acylcarnitine 18:1", pattern = "Acylcarnitine",replacement = "CAR")
# stringr::str_replace("TG 48:2 [SIM] Results", pattern = " Results",replacement = "")
# stringr::str_replace("112_BQC_A9334.d", pattern = ".d",replacement = "")
#
# stringr::str_to_lower("CE 18:1")
#
# Sample_ID <- 1
# try(Sample-ID <- 1)
#
# d_subset |> filter(AnalysisID == "149_BQC_PQC17.d", str_detect(Compound, "IS|LPI") )
#
# d_subset |> filter(str_detect(AnalysisID, "BQC|TQC") , str_detect(Compound, "IS")) -> temp
```
## Convert long table to wide table format
```{r convert-long-to-wide-table}
# d_subset_wide<- d_subset |>
# pivot_wider(names_from = "Compound" ,values_from = "Area")
#
# d_subset2 <- d_subset
# d_subset2$Compound <- factor(d_subset2$Compound)
# d_subset2$AnalysisID <- factor(d_subset2$AnalysisID)
# d_subset2$AcqTimeStamp <- NULL
# d_subset2$Area2 <- d_subset2$Area
```
## Split column
```{r split-column}
# d_area <- d_area_temp |>
# separate(
# col = AnalysisID,
# into = c("seq_no", "QCtype", "sample_name"),
# sep = "_",
# extra = "merge",
# remove = FALSE) |>
# dplyr::select(seq_no, QCtype, sample_name, AnalysisID, everything())
#
# d_area
```
```{r calulcate-column-stats}
# d_BQC <- d_area |> filter(QCtype == "BQC")
#
#
# d_res1 <- d_BQC |>
# summarise(
# across(.cols = -seq_no:-AcqTimeStamp,
# .fns = ~ sd(.)/mean(.)*100)
# )
# d_res1
#
# d_BQC_areas <- d_BQC |> dplyr::select(-seq_no:-AcqTimeStamp)
#
# d_res2 <- purrr::map_df(d_BQC_areas, .f = ~ sd(.)/mean(.)*100)
# d_res2
#
# d_res3 <- sapply(X = d_BQC_areas,
# FUN = function(x) c(CV = sd(x)/mean(x) * 100,
# RobustCV = mad(x)/median(x) * 100))
# as.data.frame(d_res3)
# as.data.frame(t(d_res3))
#
# d_BQC_long <- d_BQC |> pivot_longer(cols = -seq_no:-AcqTimeStamp,
# names_to = "Compound",
# values_to = "Area")
#
# d_BQC_stats <- d_BQC_long |>
# group_by(Compound) |>
# summarise(
# count = n(),
# Mean = mean(Area),
# Min = min(Area),
# CV = sd(Area)/mean(Area) *100,
# logCV = sqrt(exp(1)^(sd(log(Area))^2)-1) *100,
# logCV_roche = sqrt(10^(log(10)*sd(log(Area, 10))^2)-1) *100,
# rCVq = 0.75 * IQR(Area, na.rm = TRUE)/median(Area) *100,
# rCVm = mad(Area, constant = 1.4826)/median(Area) *100
# )
# d_BQC_stats
#
# hist(d_BQC_stats$CV)
# hist(d_BQC_stats$rCVm)
#
# ggplot(d_BQC_stats) +
# geom_histogram(aes(x=CV))
#
# d_BQC_stats_long <- d_BQC_stats |>
# dplyr:::select(Compound, CV,rCVm,,rCVq, logCV) |>
# pivot_longer(cols = -Compound, names_to= "CV_type" ,values_to = "Value")
# d_BQC_stats_long
#
# ggplot(d_BQC_stats_long) +
# geom_histogram(aes(x=Value, fill = CV_type)) + scale_x_continuous(limits = c(0,150)) + facet_wrap(~CV_type)
#
#
# plot(d_BQC_stats$CV, d_BQC_stats$logCV)
# plot(d_BQC_stats$CV, d_BQC_stats$logCV, xlim = c(0,100), ylim = c(0,100))
# plot(d_BQC_stats$CV, d_BQC_stats$rCVm)
# plot(d_BQC_stats$logCV, d_BQC_stats$rCVm, xlim = c(0,200))
# plot(d_BQC_stats$CV, d_BQC_stats$rCVq)
```