I have a data frame containing a column called Values
among other columns within which I want to make a comparison of every consecutive 2 rows for minimum value, print TRUE
to a row that has the minimum and FALSE
to the other row that is maximum. My row consideration is in 1,2
, 3,4
, 5,6
, 7,8
, 9,10
,11,12
, etc
. In such, my row-wise comparison is odd and even
and not even row with odd row.
library(reshape2)
set.seed(199)
MB_RMSE_sd1 <- runif(12, min = 0, max = 2)
TMB_RMSE_sd1 <- runif(12, min = 0, max = 2)
MB_RMSE_sd3 <- runif(12, min = 2, max = 5)
TMB_RMSE_sd3 <- runif(12, min = 2, max = 5)
MB_RMSE_sd5 <- runif(12, min = 5, max = 10)
TMB_RMSE_sd5 <- runif(12, min = 5, max = 10)
MB_RMSE_sd10 <- runif(12, min = 7, max = 16)
TMB_RMSE_sd10 <- runif(12, min = 7, max = 16)
MB_MAE_sd1 <- runif(12, min = 0, max = 2)
TMB_MAE_sd1 <- runif(12, min = 0, max = 2)
MB_MAE_sd3 <- runif(12, min = 2, max = 5)
TMB_MAE_sd3 <- runif(12, min = 2, max = 5)
MB_MAE_sd5 <- runif(12, min = 5, max = 10)
TMB_MAE_sd5 <- runif(12, min = 5, max = 10)
MB_MAE_sd10 <- runif(12, min = 7, max = 16)
TMB_MAE_sd10 <- runif(12, min = 7, max = 16)
ID <- rep(rep(c("N10_AR0.8", "N10_AR0.9", "N10_AR0.95", "N15_AR0.8", "N15_AR0.9", "N15_AR0.95", "N20_AR0.8", "N20_AR0.9", "N20_AR0.95", "N25_AR0.8", "N25_AR0.9", "N25_AR0.95"), 2), 1)
df1 <- data.frame(ID, MB_RMSE_sd1, TMB_MAE_sd1, MB_RMSE_sd3, TMB_MAE_sd3, MB_RMSE_sd5, TMB_MAE_sd5, MB_RMSE_sd10, TMB_MAE_sd10)
reshapp1 <- reshape2::melt(df1, id = "ID")
NEWDAT <- data.frame(value = reshapp1$value, year = reshapp1$ID, n = rep(rep(c("10", "15", "20", "25"), each = 3), 16), Colour = rep(rep(c("RMSE_MB", "RMSE_TMB", "MAE_MB", "MAE_TMB"), each = 12), 4), sd = rep(rep(c(1, 3, 5, 10), each = 48), 1), phi = rep(rep(c("0.8", "0.9", "0.95"), 16), 4))
NEWDAT$sd <- with(NEWDAT, factor(sd, levels = sd, labels = paste("sd =", sd)))
NEWDAT$year <- factor(NEWDAT$year, levels = NEWDAT$year[1:12])
NEWDAT$n <- with(NEWDAT, factor(n, levels = n, labels = paste("n = ", n)))
##################################
head(NEWDAT)
# value year n Colour sd phi
# 1 0.05624369 N10_AR0.8 n = 10 RMSE_MB sd = 1 0.8
# 2 1.32718190 N10_AR0.9 n = 10 RMSE_MB sd = 1 0.9
# 3 1.42121934 N10_AR0.95 n = 10 RMSE_MB sd = 1 0.95
# 4 0.56366171 N15_AR0.8 n = 15 RMSE_MB sd = 1 0.8
# 5 0.02666847 N15_AR0.9 n = 15 RMSE_MB sd = 1 0.9
# 6 0.48640038 N15_AR0.95 n = 15 RMSE_MB sd = 1 0.95
What I Want
NEWDAT1 <- dplyr::mutate(NEWDAT, Highlight = value == min...)
head(NEWDAT1)
# value year n Colour sd phi Highlight
# 1 0.05624369 N10_AR0.8 n = 10 RMSE_MB sd = 1 0.8 TRUE
# 2 1.32718190 N10_AR0.9 n = 10 RMSE_MB sd = 1 0.9 FALSE
# 3 1.42121934 N10_AR0.95 n = 10 RMSE_MB sd = 1 0.95 FALSE
# 4 0.56366171 N15_AR0.8 n = 15 RMSE_MB sd = 1 0.8 TRUE
# 5 0.02666847 N15_AR0.9 n = 15 RMSE_MB sd = 1 0.9 TRUE
# 6 0.48640038 N15_AR0.95 n = 15 RMSE_MB sd = 1 0.95 FALSE
CodePudding user response:
library(tidyverse)
df2 <- NEWDAT %>%
mutate(index = row_number()) %>%
mutate(Highlight = case_when(index %% 2 != 0 & value < lead(value) ~ TRUE,
index %% 2 == 0 & value < lag(value) ~ TRUE,
TRUE ~ FALSE))
> df2
value year n Colour sd phi index Highlight
1 0.05624369 N10_AR0.8 n = 10 RMSE_MB sd = 1 0.8 1 TRUE
2 1.32718190 N10_AR0.9 n = 10 RMSE_MB sd = 1 0.9 2 FALSE
3 1.42121934 N10_AR0.95 n = 10 RMSE_MB sd = 1 0.95 3 FALSE
4 0.56366171 N15_AR0.8 n = 15 RMSE_MB sd = 1 0.8 4 TRUE
5 0.02666847 N15_AR0.9 n = 15 RMSE_MB sd = 1 0.9 5 TRUE
6 0.48640038 N15_AR0.95 n = 15 RMSE_MB sd = 1 0.95 6 FALSE
7 1.37879656 N20_AR0.8 n = 20 RMSE_MB sd = 1 0.8 7 FALSE
8 1.33491011 N20_AR0.9 n = 20 RMSE_MB sd = 1 0.9 8 TRUE
9 0.56152963 N20_AR0.95 n = 20 RMSE_MB sd = 1 0.95 9 TRUE
10 0.74926950 N25_AR0.8 n = 25 RMSE_MB sd = 1 0.8 10 FALSE