Last active
September 30, 2022 11:04
-
-
Save nrkoehler/965b6466a0bf7261b05d8e63b1c41e88 to your computer and use it in GitHub Desktop.
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
# Flatten a list returned by the rle() function | |
library(purrr) | |
# ARRAY ------------------------------------------------------------------- | |
test_array <- rbind(c(T,T,T,F,F,T,F,T,T), | |
c(T,T,F,F,F,T,F,T,T), | |
c(T,T,T,F,T,T,T,T,T)) | |
test_rle <- apply(test_array, 1, rle) | |
l1 <- flatten_int(map(test_rle, pluck, 'lengths')) | |
v1 <- flatten_lgl(map(test_rle, pluck, 'values')) | |
rep(v1, l1) | |
# LIST -------------------------------------------------------------------- | |
test_list <- list(c(T,T,T,F,F,T,F,T,T), | |
c(T,T,F,F,F,T,F,T,T), | |
c(T,T,T,F,T,T,T,T,T)) | |
test2_rle <- map(test_list, rle) | |
l2 <- flatten_int(map(test2_rle, pluck, 'lengths')) | |
v2 <- flatten_lgl(map(test2_rle, pluck, 'values')) | |
# length of runs | |
lr2 <- map(test2_rle, pluck, 'lengths') %>% | |
map_int(., length) | |
# get numbers of runs | |
r2 <- test2_rle %>% | |
set_names(., 1:length(lr2)) %>% | |
names() | |
dfr <- data.frame( | |
run = rep(r2, lr2), | |
lengths = l2, | |
values = v2 | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment