# Attach packages
library(rearrr)
library(dplyr)
has_ggplot <- require(ggplot2) # Attach if installed
# Set seed
set.seed(1)
# Create a data frame
df <- data.frame(
"Index" = 1:10,
"A" = sample(1:10),
"B" = runif(10),
"G" = c(
1, 1, 1, 2, 2,
2, 3, 3, 3, 3
),
stringsAsFactors = FALSE
)
# Flip values of the columns
flip_values(df$A)
flip_values(df, cols = "A")
flip_values(df, cols = "B", origin = 0.3, origin_fn = NULL, keep_original = FALSE)
flip_values(df,
cols = c("A", "B"),
origin = c(3, 0.3),
origin_fn = NULL,
suffix = "",
keep_original = FALSE,
overwrite = TRUE
)
flip_values(df, cols = c("A", "B"), origin_fn = create_origin_fn(max))
# Grouped by G
df %>%
dplyr::group_by(G) %>%
flip_values(
cols = c("A", "B"),
origin_fn = create_origin_fn(median),
keep_original = FALSE
)
# Plot A and flipped A
# First flip A around the median and then around the value 3.
df <- df %>%
flip_values(cols = "A", suffix = "_flip_median", origin_col_name = NULL) %>%
flip_values(cols = "A", suffix = "_flip_3", origin = 3,
origin_fn = NULL, origin_col_name = NULL)
# Plot A and A flipped around its median
if (has_ggplot){
ggplot(df, aes(x = Index, y = A)) +
geom_line(aes(color = "A")) +
geom_line(aes(y = A_flip_median, color = "Flipped A (median)")) +
geom_hline(aes(color = "Median A", yintercept = median(A))) +
theme_minimal()
}
# Plot A and A flipped around the value 3
if (has_ggplot){
ggplot(df, aes(x = Index, y = A)) +
geom_line(aes(color = "A")) +
geom_line(aes(y = A_flip_3, color = "Flipped A (3)")) +
geom_hline(aes(color = "3", yintercept = 3)) +
theme_minimal()
}
Run the code above in your browser using DataLab