color_if | R Documentation |
Convenient function that formats columns in data frames with color codes, where the color is chosen based on certain conditions. Columns are then printed in color in the console.
color_if(
x,
columns,
predicate = `>`,
value = 0,
color_if = "green",
color_else = "red",
digits = 2
)
colour_if(
x,
columns,
predicate = `>`,
value = 0,
colour_if = "green",
colour_else = "red",
digits = 2
)
x |
A data frame |
columns |
Character vector with column names of |
predicate |
A function that takes |
value |
The comparator. May be used in conjunction with |
color_if , colour_if |
Character vector, indicating the color code used to
format values in |
color_else , colour_else |
See |
digits |
Digits for rounded values. |
The predicate-function simply works like this:
which(predicate(x[, columns], value))
x
, where columns matched by predicate
are wrapped into color codes.
# all values in Sepal.Length larger than 5 in green, all remaining in red
x <- color_if(iris[1:10, ], columns = "Sepal.Length", predicate = `>`, value = 5)
x
cat(x$Sepal.Length)
# all levels "setosa" in Species in green, all remaining in red
x <- color_if(iris, columns = "Species", predicate = `==`, value = "setosa")
cat(x$Species)
# own function, argument "value" not needed here
p <- function(x, y) {
x >= 4.9 & x <= 5.1
}
# all values in Sepal.Length between 4.9 and 5.1 in green, all remaining in red
x <- color_if(iris[1:10, ], columns = "Sepal.Length", predicate = p)
cat(x$Sepal.Length)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.