-
Notifications
You must be signed in to change notification settings - Fork 0
/
session-04.R
143 lines (120 loc) · 3 KB
/
session-04.R
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
analyze <- function(filename) {
dat <- read.csv(file = filename, header=FALSE)
ave_day_inflammation <- apply(dat, 2, mean)
plot(ave_day_inflammation)
max_day_inflammation <- apply(dat, 2, max)
plot(max_day_inflammation)
min_day_inflammation <- apply(dat, 2, min)
plot(min_day_inflammation)
}
filenames <- list.files(path = "data",
pattern = "inflammation",
full.names = TRUE)
for (f in filenames) {
print(f)
analyze(f)
}
analyze_all <- function(pattern) {
filenames <- list.files(path = "data",
pattern = pattern,
full.names = TRUE)
for (f in filenames) {
print(f)
analyze(f)
}
}
analyze_all("inflammation")
pdf("inflammation-01.pdf")
analyze("data/inflammation-01.csv")
dev.off()
num <- 100
if (num > 100) {
print("greater")
} else {
print("not greater")
}
num > 100
num < 100
num == 100
num <- 37
if (num > 100) {
print("greater")
}
# function that determines the sign of a
# number
sign <- function(num) {
if (num > 0) {
# do thing 1
# do thing 2
# do thing 3
return(1)
} else if (num == 0) {
return(0)
} else {
return(-1)
}
}
sign(100)
sign(0)
sign(-567)
# "and" -> &
# "or"-> |
10 > 20 | 10 > 4 # TRUE
10 > 20 & 10 > 4 # FALSE
# Exclusive
x <- 1:10
x > 5
dat <- read.csv("data/inflammation-01.csv", header = FALSE)
# length is a function to give the lenght of a vector
plot_dist <- function(x, threshold) {
if (length(x) > threshold) {
boxplot(x)
} else {
stripchart(x)
}
}
plot_dist(dat[, 10], threshold = 10) # day (column) 10
plot_dist(dat[1:5, 10], threshold = 10) # samples (rows) 1-5 on day (column) 10
# Plots the average, min, and max inflammation over days.
# Input:
# filename: character string of csv file
# output: character string of pdf file to save plots
#
analyze <- function(filename, output = NULL) {
if (!is.null(output)) {
pdf(output)
}
dat <- read.csv(file = filename, header=FALSE)
ave_day_inflammation <- apply(dat, 2, mean)
plot(ave_day_inflammation, type = "l", col = "red")
max_day_inflammation <- apply(dat, 2, max)
plot(max_day_inflammation, type = "l", col = "red")
min_day_inflammation <- apply(dat, 2, min)
plot(min_day_inflammation, type = "l", col = "red")
if (!is.null(output)) {
dev.off()
}
}
analyze("data/inflammation-01.csv")
analyze("data/inflammation-01.csv", output = "test.pdf")
dir.create("results")
analyze("data/inflammation-01.csv",
output = "results/inflammation-01.pdf")
f <- "inflammation-01.csv"
sub("csv", "pdf", f)
file.path("results", f)
analyze_all <- function(pattern) {
data_dir <- "data"
results_dir <- "results"
filenames <- list.files(path = data_dir,
pattern = pattern)
for (f in filenames) {
print(f)
pdf_name <- file.path(results_dir, sub("csv", "pdf", f))
csv_name <- file.path(data_dir, f)
analyze(csv_name, output = pdf_name)
}
}
analyze_all("inflammation")
stopifnot(file.exists(f))
tryCatch()