Created
July 6, 2021 17:38
-
-
Save MCMaurer/95e7003edb62ef65e7d23b850ff1de1e to your computer and use it in GitHub Desktop.
pairwise column differences
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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