R/fct_helpers.R

Defines functions flip.cage.file.func

#' helpers 
#'
#' @description A fct function
#'
#' @return The return value, if any, from executing the function.
#'
#' @noRd

flip.cage.file.func <- function(f){
  library(tidyverse)
  
  f %>% 
    # not sure if always the first 6 columns
    pivot_longer(cols = -c(1:6), names_to = 'var', values_to = 'n') %>% 
    # prob with kcal_hr_cage# so just remove the first _
    mutate(var = gsub('kcal_hr','kcalhr',var)) %>% 
    # split up the var column into var and cage_id
    separate(var, into = c('var','cage_id'), sep = "_") %>% 
    janitor::clean_names() %>% 
    # make cage_id numeric
    mutate(cage_id = as.numeric(cage_id)) %>% 
    # fix datetime column
    mutate(day = as.Date(date_time_1, format = '%m/%d/%Y'),
           dt = as.POSIXct(date_time_1, format = '%m/%d/%Y %H:%M')) %>% 
    # find the start day; will need this to match with metadata
    group_by(cage_id) %>% 
    mutate(start_day = min(day),
           end_day = max(day)) %>% 
    ungroup() %>% 
    # get unique subjects because there can be cage_id duplicates
    ## Assume: no duplicate cage_id in one file
    #example: if two cage 1 they will be in different files and since this is ran per file the "start_day" should be correct
    mutate(subject_id = paste(cage_id, start_day, sep = '_'))

  
}
becky-work/MouseCageApp documentation built on Dec. 19, 2021, 7:43 a.m.