Skip to content

Instantly share code, notes, and snippets.

@MCMaurer
Created July 6, 2021 17:38
Show Gist options
  • Save MCMaurer/95e7003edb62ef65e7d23b850ff1de1e to your computer and use it in GitHub Desktop.
Save MCMaurer/95e7003edb62ef65e7d23b850ff1de1e to your computer and use it in GitHub Desktop.
pairwise column differences
library(tidyverse)
library(tidybayes)
d <- tibble(sensor_a = rnorm(100, 5, 2),
sensor_b = rnorm(100, 1, 1),
sensor_c = rnorm(100, 0, 3),
time = 1:100)
# pivot longer so all our sensor names are in one column, and readings in another
d <- d %>%
pivot_longer(starts_with("sensor"), names_to = "sensor", values_to = "reading")
d
# now we group by time, because we want comparisons at each time point. Then we "compare levels" of our readings by each sensor
d_diff <- d %>%
group_by(time) %>%
compare_levels(variable = reading, by = sensor)
# now we've got all these differences!
d_diff
d_diff %>%
ggplot(aes(x = time, y = reading)) +
geom_line() +
facet_wrap(vars(sensor), scales = "free_y")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment