Basics
rescale01 <- function(x) {
rng <- range(x, na.rm = TRUE, finite = TRUE) #The option finite = TRUE to range() will drop all non-finite elements, and NA is a non-finite element.
y <- (x - rng[1]) / (rng[2] - rng[1])
y[y == -Inf] <- 0
y[y == Inf] <- 1
y
}
rescale01(c(Inf, -Inf, 0:5, NA))
#> [1] 1.0 0.0 0.0 0.2 0.4 0.6 0.8 1.0 NA
#name, argument and body are needed for a function
#the last expression is the value that returns by default;use `return()` to specially return
commas <- function(...) stringr::str_c(..., sep = ", ", collapse = ", ")
commas(letters[1:10])
#use `...` to set the input arbitrary values
ifelse
if (condition) {
# code executed when condition is TRUE
} else {
# code executed when condition is FALSE
}
#`else{}` can be omitted`
#multiple conditions
x <- 3L
if (identical(x, 2L)) {
cat("x is integer 2")
} else if (identical(x, 3L)) {
cat("x is integer 3")
} else {
cat("x is neither integer 2 nor integer 3")
}
#a simplified version
ifelse(cond,s1,s2) #if cond is T,execute s1,else execute s2
map
##use `map()` to directly compute the values and form a list
df <- tibble(
a = rnorm(10),
b = rnorm(10),
c = rnorm(10),
d = rnorm(10)
)
map(df, mean) # the second parameter denotes the function it uses
map(df, median)
#`map_dbl()`,`map_int()` and so on form a vector
##simulate some random normals with different means
mu <- list(5, 10, -3)
mu %>%
map(rnorm, n = 5) %>%
str()
##simulate some random normals with different means and sds
mu <- list(5, 10, -3)
sigma <- list(1, 5, 10)
map2(mu, sigma, rnorm, n = 5) %>% str() #`map2` receives two lists as parameters
##simulate some random normals with different means, sds and numbers of samples
n <- list(1, 3, 5)
mu <- list(5, 10, -3)
sigma <- list(1, 5, 10)
args2 <- list(n = n, mean = mu, sd = sigma)
args2 %>%
pmap(rnorm) %>%
str()
switch(omitted for test)
switch(3,2*3,sd(1:5),runif(3))
#[1] 0.13854046 0.04598363 0.06802463
#the first value denotes which one to execute
for Circulation(omitted for test)
df <- tibble(
a=rnorm(10),
b=rnorm(10),
c=rnorm(10),
d=rnorm(10)
)
#compute the medians of every column
output <- vector("double",ncol(df)) #preserve space for the vector beforehand
for (i in seq_along(df)) { #`seq_along()` is similar to `1:length()`,but more safe
output[[i]] <- median(df[[i]])
}
output
##ncol() returns to the number of columns of the dataset
##in `vector`,the first parameter can be `character`,`integer`...and `list`
#generate 10 numbers derived from the normal distributions which means are -10,0,10,and 100
mn<-c(-10,0,10,100)
nd_list <- vector("list",length(mn))
for (i in seq_along(mn)) {
nd_list[[i]] <-rnorm(10,mn[[i]])
}
str(nd_list)
##use `str(unlist(nd_list))` to collapse the list into a single vector
while(omitted for test)
while(cond){
#circulation body
}
#find how many tries it takes to get three heads in a row when flipping a coin
flip <- function() sample(c("T", "H"), 1) # return T or H
flips <- 0
nheads <- 0
while (nheads < 3) {
if (flip() == "H") {
nheads <- nheads + 1
} else {
nheads <- 0
}
flips <- flips + 1
}
flips
Parameter
#create a function to compute t-statistics
sim.t <- function(n,me=0,std=1){
x <- rnorm(n,me,std)
(mean(x)-me)/(sd(x)/sqrt(n))
}
sim.t(10)
sim.t(10,5,2)
#values of parameters are by default the original ones when creating the function;it is practical to input different values if needed
Special
temp <- seq(-10, 50, by = 5)
cut(temp, c(-Inf, 0, 10, 20, 30, Inf),
right = FALSE, #To have intervals open on the left (using <)
labels = c("freezing", "cold", "cool", "warm", "hot")
)
#> [1] freezing freezing cold cold cool cool warm warm
#> [9] hot hot hot hot hot
#> Levels: freezing cold cool warm hot