# Attach packages
library(rearrr)
# Set seed
set.seed(1)
# Create three vectors
x <- runif(10)
y <- runif(10)
z <- runif(10)
# Create n_fn that gets the median index
# and rounds down with floor()
median_index_fn <- create_n_fn(median, use_index = TRUE, round_fn = floor)
# Use median_index_fn
median_index_fn(x, y, z)
# Create n_fn that gets the median of each dimension
median_n_fn <- create_n_fn(median)
# Use median_origin_fn
median_n_fn(x, y, z)
# Should be the same as
round(c(median(x), median(y), median(z)))
# Use mean and ignore missing values
mean_n_fn <- create_n_fn(mean, na.rm = TRUE)
# Add missing values
x[[2]] <- NA
y[[5]] <- NA
# Use mean_n_fn
mean_n_fn(x, y, z)
# Should be the same as
round(c(
mean(x, na.rm = TRUE),
mean(y, na.rm = TRUE),
mean(z, na.rm = TRUE)
))
Run the code above in your browser using DataLab