Created
March 12, 2019 04:57
-
-
Save benzipperer/e1f6ef886bec6ccb2a97f7ab69ff9d7a to your computer and use it in GitHub Desktop.
spells in R
This file contains hidden or 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(dplyr) | |
| # just used to simulate some data | |
| library(purrr) | |
| # create fake data | |
| f <- function(x,i) { | |
| return(data.frame(bout=x, element=i)) | |
| } | |
| fake_data <- map_dfr(1:4, ~map2_dfr(.x, 0:(round(runif(1)*10)+5), f)) %>% | |
| mutate(invisible_id=ifelse(element==0,"bout","element")) %>% | |
| mutate(coolmeasurement = runif(n())) %>% | |
| select(invisible_id,coolmeasurement) | |
| fake_data | |
| # calculate runs | |
| rle_list <- rle(fake_data$invisible_id) | |
| # identify run | |
| rle_list$values <- 1:length(rle_list$values) | |
| # attach runs to data | |
| fake_data$bout_id <- inverse.rle(rle_list) | |
| # give bout & element the same bout_id | |
| fake_data <- fake_data %>% mutate(bout_id = ifelse((bout_id %% 2) == 1, bout_id + 1, bout_id)) | |
| # now make bout_id run from 1:N | |
| fake_data <- fake_data %>% mutate(bout_id = bout_id / 2) | |
| fake_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment