Using DF <- iris[-5]as sample data, you can use cut, as I suggested in the comments.
Try:
DF[] <- lapply(DF, cut, c(-Inf, -2, 2, Inf), c("down", "no_change", "up"))
head(DF)
#
#
#
#
#
#
#
tail(DF)
#
#
#
#
#
#
#
, RHertel "mock_data":
cut(mock_data, c(-Inf, -2, 2, Inf), c("down", "no_change", "up"))
#
#
#
#
, RHertel, , . ( ) factor ( ).
, , :
set.seed(1)
nrow = 20000
ncol = 1000
x <- as.data.frame(matrix(runif(nrow * ncol, min=-5, max=5), ncol = ncol))
factorize <- function(invec) {
factorized <- rep("no_change", length(invec))
factorized[invec > 2] <- "up"
factorized[invec < -2] <- "down"
factor(factorized, c("down", "no_change", "up"))
}
RHfun <- function(indf = x) {
indf[] <- lapply(indf, factorize)
indf
}
AMfun <- function(DF = x) {
DF[] <- lapply(DF, cut, c(-Inf, -2, 2, Inf), c("down", "no_change", "up"))
DF
}
library(microbenchmark)
microbenchmark(AMfun(), RHfun(), times = 10)