-
Notifications
You must be signed in to change notification settings - Fork 0
/
loops_conditions.qmd
198 lines (131 loc) · 3.76 KB
/
loops_conditions.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
---
bibliography: references.bib
number-sections: true
suppress-bibliography: false
---
# Loops and Conditions in R
```{r}
#| results: "asis"
#| echo: false
source("_common.R")
```
## Introduction
Importing data is often the first step in a data analysis project. Quality and integrity of imported data is key for the subsequent analysis - *Quality In Quality Out*. A good overview of data import in R helps to ensure integrity, completeness and efficient import of data .
## Libraries
```{r}
#| label: load-lib
#| echo: true
#| message: false
library(here)
library(tidyverse)
library(readxl)
```
## For loops
```{r}
#| label: for_loops
#| echo: true
#| message: false
for (i in c(1,2,3,4,5)) {
print(paste0("here number ", i, "-)"))
print(glue("here number {i}"))
}
```
## Import multiple sheets from an EXCEL file
First let's test importing one sheet. We need the name of the file and sheet
```{r}
#| label: read-xlsx
#| echo: false
#| message: true
mysheet1 <- readxl::read_excel(path = here("data/Test.xlsx"), sheet = "Sheet1")
```
Now we get a list of sheets in an Excel file
```{r}
#| label: read-xlsx
#| echo: false
#| message: true
my_sheets <- readxl::excel_sheets(here("data/Test.xlsx"))
my_sheets
```
Now we import all of the sheets using a for loop
```{r}
#| label: loop-through-sheets
#| echo: false
#| message: true
for (sheet_name in my_sheets) {
mysheet <- readxl::read_excel(path = here("data/Test.xlsx"), sheet = sheet_name)
print(mysheet)
}
```
However, while each sheet is imported and was printed out, the tables have not been joined yet.
```{r}
#| label: loop-through-sheets
#| echo: false
#| message: true
for (sheet_name in my_sheets) {
mysheet <- readxl::read_excel(path = here("data/Test.xlsx"), sheet = sheet_name)
print(mysheet)
}
```
```{r}
#| label: loop-through-sheets
#| echo: false
#| message: true
# in the first loop data will be stacked to an empty dataframe. So we need to define it first
final_table <- tibble()
for (sheet_name in my_sheets) {
mytable <- readxl::read_excel(path = here("data/Test.xlsx"), sheet = sheet_name)
# Add sheet name as column
mytable <- mytable |> mutate(sheet = sheet_name, .before = ID)
# Stack the table to the current stacked
final_table <- bind_rows(final_table, mysheet)
}
```
```{r}
# define function
get_my_table <- function(filename, sheetname) {
mysheet <- readxl::read_excel(filename, sheet = sheetname)
mysheet <- mysheet |> mutate(sheet = sheet_name, .before = ID)
# return result by calling it
mysheet
}
my_sheets <- readxl::excel_sheets(here("data/Test.xlsx"))
for (sheet_name in my_sheets) {
mysheet <- get_my_table(filename = here("data/Test.xlsx") ,sheetname = sheet_name)
final_table <- bind_rows(final_table, mysheet)
}
```
```{r}
library(purrr)
myfunction <- function(x){print(x^2)}
a <- purrr::walk(.x = c(1,2,3,4,5), .f = ~ myfunction(.x))
my_sheets <- readxl::excel_sheets(here("data/Test.xlsx"))
# Define function
get_my_table <- function(filename, sheetname) {
mysheet <- readxl::read_excel(filename, sheet = sheetname)
mysheet <- mysheet |> mutate(sheet = sheet_name, .before = ID)
# return result by calling it
mysheet
}
#walk along the list of sheets, import them and bind them automatically
purrr::map_df(.x = my_sheets ,.f = ~get_my_table(here("data/test.xlsx"),sheetname = .x))
```
```{r}
sometext = "RQC"
if (str_detect(sometext, "TQC")) {
print("Yes TQC")
} else if (str_detect(sometext, "BQC")) {
print("yes BQC")
} else {
print("nothing")
}
#...mutate( conc = if_else(tissue = "cells", conc *1000, conc)
ifelse("cells" == NA, 1,2)
qctype <- "PQC"
conc <- case_when(
qctype == "TQC" ~ 0,
qctype == "BQC" ~ 10,
qctype == "RQC" ~ 100,
qctype == "SPL" ~ 1000,
TRUE ~ 1111
)
```