Bioinformatics - R
> runif(3)
0.7914955 0.3977714 0.6957978
> runif(3)
0.77689274 0.09369743 0.21302195
> runif(3)
0.3468565 0.5167525 0.8573418
> runif(3)
? ? ?
1
HS16 | UniBas | JCW
Bioinformatics - R
> set.seed(161006)
> runif(3)
0.6526515 0.2767145 0.9589001
> set.seed(161006)
> runif(3)
0.6526515 0.2767145 0.9589001
> set.seed(161006)
> runif(3)
? ? ?
2
HS16 | UniBas | JCW
Bioinformatics - R
Set the seed of R‘s random number generator, which is
useful for creating simulations or random objects that
can be reproduced.
> set.seed()
3
HS16 | UniBas | JCW
Bioinformatics - R
f <- function(x) {
A <- sqrt(x)
B <- x^2
}
> f(2)
?
> A
?
4
HS16 | UniBas | JCW
Bioinformatics - R
f <- function(x) {
A <- sqrt(x)
B <- x^2
print(c(A,B))
}
> f(4)
?
5
HS16 | UniBas | JCW
Bioinformatics - R
f <- function(x) {
A <- sqrt(x)
B <- x^2
print(c(A,B))
}
> f(4)
2 16
6
HS16 | UniBas | JCW
Bioinformatics - R
f <- function(x) {
A <- sqrt(x)
B <- x^2
print(c(A,B))
}
> f(-4)
?
7
HS16 | UniBas | JCW
Bioinformatics - R
f <- function(x) {
A <- sqrt(x)
B <- x^2
print(c(A,B))
}
> f(-4)
NaN 16
Warning message:
In sqrt(x) : NaNs produced
8
HS16 | UniBas | JCW
Bioinformatics - R
f <- function(x) {
A <- sqrt(ifelse(x >= 0, x, NA))
B <- x^2
print(c(A,B))
}
> f(-4)
?
9
HS16 | UniBas | JCW
Bioinformatics - R
f <- function(x) {
A <- sqrt(ifelse(x >= 0, x, NA))
B <- x^2
print(c(A,B))
}
> f(-4)
NA 16
10
HS16 | UniBas | JCW
Bioinformatics - R
go <- function(x, menu) {
switch(menu,
a = (x <- x + 10),
b = (x <- x - 10),
c = (if(x < 5) { x <- x * 2 } else {x <- y}) )
}
> go(5)
?
11
HS16 | UniBas | JCW
Bioinformatics - R
go <- function(x, menu) {
switch(menu,
a = (x <- x + 10),
b = (x <- x - 10),
c = (if(x < 5) { x <- x * 2 } else {x <- y}) )
}
> y <- 99
> go(5, “b”)
> ?
12
HS16 | UniBas | JCW
Bioinformatics - R
go <- function(x, menu) {
switch(menu,
a = (x <- x + 10),
b = (x <- x - 10),
c = (if(x < 5) { x <- x * 2 } else {x <- y}) )
}
> y <- 99
> go(5, “b”)
> -5
13
HS16 | UniBas | JCW
Bioinformatics - R
go <- function(x, menu) {
switch(menu,
a = (x <- x + 10),
b = (x <- x - 10),
c = (if(x < 5) { x <- x * 2 } else {x <- y}) )
}
> y <- 99
> go(5, “c”)
> ?
14
HS16 | UniBas | JCW
Bioinformatics - R
go <- function(x, menu) {
switch(menu,
a = (x <- x + 10),
b = (x <- x - 10),
c = (x <- y))
}
> y <- 99
> go(5, “c”)
> 99
15
HS16 | UniBas | JCW
Bioinformatics - R
cf <- function(x, c){
if(c == "C2F") return(9/5*x + 32)
if(c == "F2C") return((x-32)*5/9)
else return("Not available")
}
> a <- cf(25, “C2F”)
> cf(a, “F2C”)
> ?
16
HS16 | UniBas | JCW
Bioinformatics - R
cf <- function(x, c){
if(c == "C2F") return(9/5*x + 32)
if(c == "F2C") return((x-32)*5/9)
else return("Not available")
}
> a <- cf(25, “C2F”)
> cf(a, “F2C”)
> 25
17
HS16 | UniBas | JCW
Bioinformatics - R
alpha.plot <- function(phyloseq, alpha) {
switch(typ,
s = (plot_richness(d, measures="Shannon")),
c = (plot_richness(d, measures="Chao")),
o = (plot_richness(d, measures=“Observed”)))
}
> alpha.plot(x, s)
Error? Why do I not get a nice alphadiversity plot with Simpson measures?
18
HS16 | UniBas | JCW
Bioinformatics - R
alpha.plot <- function(phyloseq, alpha) {
switch(alpha,
sh = (plot_richness(d, measures="Shannon")),
ch = (plot_richness(d, measures="Chao")),
ob = (plot_richness(d, measures=“Observed”)),
si = (plot_richness(d, measures=“Simpson”)))
}
> alpha.plot(x, “si”)
19
HS16 | UniBas | JCW
© Copyright 2026 Paperzz