Return index of entry nearest to a value:
nearest <- function(value, dataset) {
which(abs(dataset-value)==min(abs(dataset-value)))
}
nearest(pi, 10:1)
Append standard deviation to variable summary:
summary0 <- function(x) {
print(summary(x));
cat("Std.Dev.", "\n", sd(x), "\n")
}
summary0(rnorm(10))
Recode existing variable into percentile scores / percentile ranks:
percentile <- function(x) {
x <- (x - min(x)) / (max(x) - min(x))
}
x1 <- rnorm(100)
x2 <- percentile(x1)
plot(x1,x2)
See also this post: All things random: Change function defaults in R
No comments:
Post a Comment