---
title: "AutoResp data import adjusted for Background respiration"
output:
  word_document: default
  pdf_document: default
  html_document:
    df_print: paged
---
Not sure which of these are necessary
```{r}
library(rMR)
library(respirometry)
library(PKNCA)
library(biglm)
library(DBI)
library(bReeze)
library(reshape2)
library(dplyr)
library(lme4)
library(car)
library(data.table)
library(ggplot2)
library(readxl)
library(dbplyr)
library(tidyverse)
library(tidyr)
library(ggpubr)
```


restructure weight file
```{r}
weight_kg <- read_excel("weight_kg.xlsx")
weight_kg<-gather(weight_kg, key = "Chamber", value = "Weight(kg)", Chamber1,Chamber2,Chamber3,Chamber4)
weight_kg <- na.omit(weight_kg)
weight_kg<-unite(weight_kg, "date", Date:Chamber, remove = TRUE)
library("tibble") 
weight_kg<-column_to_rownames(weight_kg, var = "date")
```


```{r}
Proj4.background.format <- c("Date.time","Time(sec)","Loop","Phase.Time(sec)","CH1.mO2[mgO2/hr]","CH1.slope[mgO2/L/hr]","CH1.R2","CH1.max.O2[mgO2/L]","CH1.minO2[mgO2/L]","CH1.avg.O2[mgO2/L]","CH1.avg.temp[°C]","CH2.mO2[mgO2/hr]","CH2.slope[mgO2/L/hr]","CH2.R2","CH2.max.O2[mgO2/L]","CH2.minO2[mgO2/L]","CH2.avg.O2[mgO2/L]","CH2.avg.temp[°C]","CH3.mO2[mgO2/hr]","CH3.slope[mgO2/L/hr]","CH3.R2","CH3.max.O2[mgO2/L]","CH3.minO2[mgO2/L]","CH3.avg.O2[mgO2/L]","CH3.avg.temp[°C]","CH4.mO2[mgO2/hr]","CH4.slope[mgO2/L/hr]","CH4.R2","CH4.max.O2[mgO2/L]","CH4.minO2[mgO2/L]","CH4.avg.O2[mgO2/L]","CH4.avg.temp[°C]","avg.temp[°C]","temp.setpoint[°C]","temp.hysteresis[°C]")
```

Jan 29th (background in CH1)
```{r}
Jan29.kg<-get.witrox.data("29-01-2019_hour_17h.txt", lines.skip = 13,
                          delimit = "tab", choose.names = TRUE,
                          chosen.names = Proj4.background.format,
                          format = "%d-%m-%Y/%H:%M:%S")
    Jan29.temp<-select(Jan29.kg,33)
    Jan29.background<-select(Jan29.kg,c(2,4:5)) #this is specific to background in CH1 (extracting just the necessary columns for background)
    Jan29.background<-Jan29.background %>% separate(`Phase.Time(sec)`, c(NA,NA, "M"),fill = "left") #if it's just M makeing sure that it ends up on the right
    Jan29.background<-Jan29.background %>% separate(M, into = c("Phase", "M.sec"), sep = "(?<=[A-Za-z])(?=[0-9])")
    Jan29.background <- Jan29.background[grep("M", Jan29.background$Phase),] #if there is a phase with just a flush or a wait, make sure it is removed
    Jan29.background$M.sec<-as.numeric(Jan29.background$M.sec)
    Jan29.background<-filter(Jan29.background,M.sec>59) #removes the whole row
    Jan29.background <- na.omit(Jan29.background)
    ggplot(Jan29.background, aes(`Time(sec)`,`CH1.mO2[mgO2/hr]`)) + geom_point()+ geom_smooth(method = "lm") +   ggtitle("Jan29 Background")+ stat_cor(label.x = 2, label.y = .9) + stat_regline_equation(label.x = 2, label.y = 1) + ylim(-.6, 1) #plots the background oxygen consumption
    Jan29.lm<-lm(formula = `CH1.mO2[mgO2/hr]`~ `Time(sec)`, data = Jan29.background) 
    Jan29.background$adjust<-Jan29.lm$fitted.values #add a column of the background values (that you are adjusting by) to the origional data.frame
    Jan29.background<-select(Jan29.background,c(1,5)) #not specific to background, columns extracted stay the same
    Jan29.kg <- left_join(Jan29.kg,Jan29.background, by="Time(sec)")
    Jan29.kg<-select(Jan29.kg,-c(3:4,5:11,15:18,22:25,29:32,34:35)) #removing unneeded columns (specifc to which channel has background)
#adjusting for background (labels specific to which channel has background)
  Jan29.kg$`CH2.mO2[mgO2/hr]`<-(Jan29.kg$`CH2.mO2[mgO2/hr]`- Jan29.kg$adjust) 
  Jan29.kg$`CH3.mO2[mgO2/hr]`<-(Jan29.kg$`CH3.mO2[mgO2/hr]`- Jan29.kg$adjust) 
  Jan29.kg$`CH4.mO2[mgO2/hr]`<-(Jan29.kg$`CH4.mO2[mgO2/hr]`- Jan29.kg$adjust)
#adjust for weight (creating new column with these values) (labels specific to which channel has background)
  Jan29.kg$`CH2.mO2[mgO2/kg/hr]`<-(Jan29.kg$`CH2.mO2[mgO2/hr]`/ weight_kg[c("Jan29_Chamber2"),]) 
  Jan29.kg$`CH3.mO2[mgO2/kg/hr]`<-(Jan29.kg$`CH3.mO2[mgO2/hr]`/ weight_kg[c("Jan29_Chamber3"),]) 
  Jan29.kg$`CH4.mO2[mgO2/kg/hr]`<-(Jan29.kg$`CH4.mO2[mgO2/hr]`/ weight_kg[c("Jan29_Chamber4"),])    
#reshape
  Jan29.mO2<-Jan29.kg[,c(2,15,16,17)] #not specific to different background channel, the columns extracted stay the same
  Jan29.mO2 <- rename(Jan29.mO2, CH2 = `CH2.mO2[mgO2/kg/hr]`, CH3 = `CH3.mO2[mgO2/kg/hr]`,CH4 = `CH4.mO2[mgO2/kg/hr]`) #change labels according to background
  Jan29.mO2 <- Jan29.mO2 %>% gather (CH, mgO2, -c(`Time(sec)`))
  Jan29.R2<-Jan29.kg[,c(2,5,8,11)] #not specific to different background channel the extracted columns stay the same
  Jan29.R2 <- rename(Jan29.R2, CH2 = CH2.R2, CH3 = CH3.R2 ,CH4 = CH4.R2) #change labels according to background
  Jan29.R2 <- Jan29.R2 %>% gather(CH, R2, -c(`Time(sec)`))
  Jan29.tidy<-left_join(Jan29.mO2,Jan29.R2, by = c("Time(sec)", "CH")) 
  Jan29.tidy<-drop_na(Jan29.tidy, "mgO2")
  Jan29.tidy<-filter(Jan29.tidy,R2>.95)
#extract metabolic measures  
  Jan29.SMR<- setDT(Jan29.tidy)[, list(SMR.meanBottom10pct = mean(mgO2[mgO2<=quantile(mgO2, 0.1)])), by=CH]
  Jan29.MMR <- aggregate(mgO2 ~ CH, Jan29.tidy, max)
  Jan29.MMR<-merge(Jan29.MMR,Jan29.tidy)
  Jan29.all<-merge(Jan29.SMR,Jan29.MMR)
  Jan29.all<-rename(Jan29.all,`MMR(mgO2/kg/hr)`=mgO2)
  Jan29.all<-rename(Jan29.all,Time.MMR=`Time(sec)`)
  Jan29.all<-add_column(Jan29.all, date = "Jan29")

```

Feb05 background in channel 1
```{r}
Feb05.kg<-get.witrox.data("05-02-2019_hour_17h.txt", lines.skip = 13,
                      delimit = "tab", choose.names = TRUE,
                      chosen.names = Proj4.background.format,
                      format = "%d-%m-%Y/%H:%M:%S")
    Feb05.temp<-select(Feb05.kg,33)
    Feb05.background<-select(Feb05.kg,c(2,4:5)) #this is specific to background in CH1 (extracting just the necessary columns for background)
    Feb05.background<-Feb05.background %>% separate(`Phase.Time(sec)`, c(NA,NA, "M"),fill = "left") #if it's just M makeing sure that it ends up on the right
    Feb05.background<-Feb05.background %>% separate(M, into = c("Phase", "M.sec"), sep = "(?<=[A-Za-z])(?=[0-9])")
    Feb05.background <- Feb05.background[grep("M", Feb05.background$Phase),] #if there is a phase with just a flush or a wait, make sure it is removed
    Feb05.background$M.sec<-as.numeric(Feb05.background$M.sec)
    Feb05.background<-filter(Feb05.background,M.sec>59) #removes the whole row
    Feb05.background <- na.omit(Feb05.background)
    ggplot(Feb05.background, aes(`Time(sec)`,`CH1.mO2[mgO2/hr]`)) + geom_point()+ geom_smooth(method = "lm") +   ggtitle("Feb05 Background")+ stat_cor(label.x = 2, label.y = .9) + stat_regline_equation(label.x = 2, label.y = 1) + ylim(-.6, 1) #plots the background oxygen consumption
    Feb05.lm<-lm(formula = `CH1.mO2[mgO2/hr]`~ `Time(sec)`, data = Feb05.background) 
    Feb05.background$adjust<-Feb05.lm$fitted.values #add a column of the background values that you are adjusted by to the origional data.frame
    Feb05.background<-select(Feb05.background,c(1,5))
    Feb05.kg <- left_join(Feb05.kg,Feb05.background, by="Time(sec)")
    Feb05.kg<-select(Feb05.kg,-c(3:4,5:11,15:18,22:25,29:32,34:35)) #removing unneeded columns
#adjusting for background
  Feb05.kg$`CH2.mO2[mgO2/hr]`<-(Feb05.kg$`CH2.mO2[mgO2/hr]`- Feb05.kg$adjust) 
  Feb05.kg$`CH3.mO2[mgO2/hr]`<-(Feb05.kg$`CH3.mO2[mgO2/hr]`- Feb05.kg$adjust) 
  Feb05.kg$`CH4.mO2[mgO2/hr]`<-(Feb05.kg$`CH4.mO2[mgO2/hr]`- Feb05.kg$adjust)
#adjust for weight (creating new column with these values)
  Feb05.kg$`CH2.mO2[mgO2/kg/hr]`<-(Feb05.kg$`CH2.mO2[mgO2/hr]`/ weight_kg[c("Feb05_Chamber2"),]) 
  Feb05.kg$`CH3.mO2[mgO2/kg/hr]`<-(Feb05.kg$`CH3.mO2[mgO2/hr]`/ weight_kg[c("Feb05_Chamber3"),]) 
  Feb05.kg$`CH4.mO2[mgO2/kg/hr]`<-(Feb05.kg$`CH4.mO2[mgO2/hr]`/ weight_kg[c("Feb05_Chamber4"),])    
#reshape
  Feb05.mO2<-Feb05.kg[,c(2,15,16,17)]
  Feb05.mO2 <- rename(Feb05.mO2, CH2 = `CH2.mO2[mgO2/kg/hr]`, CH3 = `CH3.mO2[mgO2/kg/hr]`,CH4 = `CH4.mO2[mgO2/kg/hr]`)
  Feb05.mO2 <- Feb05.mO2 %>% gather(CH, mgO2, -c(`Time(sec)`))
  Feb05.R2<-Feb05.kg[,c(2,5,8,11)]
  Feb05.R2 <- rename(Feb05.R2, CH2 = CH2.R2, CH3 = CH3.R2 ,CH4 = CH4.R2)
  Feb05.R2 <- Feb05.R2 %>% gather(CH, R2, -c(`Time(sec)`))
  Feb05.tidy<-left_join(Feb05.mO2,Feb05.R2, by = c("Time(sec)", "CH")) 
  #Feb05.tidy<-Feb05.tidy%>%select(-std.time.y)
  Feb05.tidy<-drop_na(Feb05.tidy, "mgO2")
  Feb05.tidy<-filter(Feb05.tidy,R2>.95)
#extract metabolic measures  
  Feb05.SMR<- setDT(Feb05.tidy)[, list(SMR.meanBottom10pct = mean(mgO2[mgO2<=quantile(mgO2, 0.1)])), by=CH]
  Feb05.MMR <- aggregate(mgO2 ~ CH, Feb05.tidy, max)
  Feb05.MMR<-merge(Feb05.MMR,Feb05.tidy)
  Feb05.all<-merge(Feb05.SMR,Feb05.MMR)
  Feb05.all<-rename(Feb05.all,`MMR(mgO2/kg/hr)`=mgO2)
  Feb05.all<-rename(Feb05.all,Time.MMR=`Time(sec)`)
  Feb05.all<-add_column(Feb05.all, date = "Feb05")

```

Feb13 (background in channel 1)
```{r}
Feb13.kg<-get.witrox.data("13-02-2019_hour_17h.txt", lines.skip = 13,
                      delimit = "tab", choose.names = TRUE,
                      chosen.names = Proj4.background.format,
                      format = "%d-%m-%Y/%H:%M:%S")
    Feb13.temp<-select(Feb13.kg,33)
    Feb13.background<-select(Feb13.kg,c(2,4:5)) #this is specific to background in CH1 (extracting just the necessary columns for background)
    Feb13.background<-Feb13.background %>% separate(`Phase.Time(sec)`, c(NA,NA, "M"),fill = "left") #if it's just M makeing sure that it ends up on the right
    Feb13.background<-Feb13.background %>% separate(M, into = c("Phase", "M.sec"), sep = "(?<=[A-Za-z])(?=[0-9])")
    Feb13.background <- Feb13.background[grep("M", Feb13.background$Phase),] #if there is a phase with just a flush or a wait, make sure it is removed
    Feb13.background$M.sec<-as.numeric(Feb13.background$M.sec)
    Feb13.background<-filter(Feb13.background,M.sec>59) #removes the whole row
    Feb13.background <- na.omit(Feb13.background)
    ggplot(Feb13.background, aes(`Time(sec)`,`CH1.mO2[mgO2/hr]`)) + geom_point()+ geom_smooth(method = "lm") +   ggtitle("Feb13 Background")+ stat_cor(label.x = 2, label.y = .9) + stat_regline_equation(label.x = 2, label.y = 1) + ylim(-.6, 1) #plots the background oxygen consumption
    Feb13.lm<-lm(formula = `CH1.mO2[mgO2/hr]`~ `Time(sec)`, data = Feb13.background) 
    Feb13.background$adjust<-Feb13.lm$fitted.values #add a column of the background values that you are adjusted by to the origional data.frame
    Feb13.background<-select(Feb13.background,c(1,5))
    Feb13.kg <- left_join(Feb13.kg,Feb13.background, by="Time(sec)")
    Feb13.kg<-select(Feb13.kg,-c(3:4,5:11,15:18,22:25,29:32,34:35)) #removing unneeded columns
#adjusting for background
  Feb13.kg$`CH2.mO2[mgO2/hr]`<-(Feb13.kg$`CH2.mO2[mgO2/hr]`- Feb13.kg$adjust) 
  Feb13.kg$`CH3.mO2[mgO2/hr]`<-(Feb13.kg$`CH3.mO2[mgO2/hr]`- Feb13.kg$adjust) 
  Feb13.kg$`CH4.mO2[mgO2/hr]`<-(Feb13.kg$`CH4.mO2[mgO2/hr]`- Feb13.kg$adjust)
#adjust for weight (creating new column with these values)
  Feb13.kg$`CH2.mO2[mgO2/kg/hr]`<-(Feb13.kg$`CH2.mO2[mgO2/hr]`/ weight_kg[c("Feb13_Chamber2"),]) 
  Feb13.kg$`CH3.mO2[mgO2/kg/hr]`<-(Feb13.kg$`CH3.mO2[mgO2/hr]`/ weight_kg[c("Feb13_Chamber3"),]) 
  Feb13.kg$`CH4.mO2[mgO2/kg/hr]`<-(Feb13.kg$`CH4.mO2[mgO2/hr]`/ weight_kg[c("Feb13_Chamber4"),])    
#reshape
  Feb13.mO2<-Feb13.kg[,c(2,15,16,17)]
  Feb13.mO2 <- rename(Feb13.mO2, CH2 = `CH2.mO2[mgO2/kg/hr]`, CH3 = `CH3.mO2[mgO2/kg/hr]`,CH4 = `CH4.mO2[mgO2/kg/hr]`)
  Feb13.mO2 <- Feb13.mO2 %>% gather(CH, mgO2, -c(`Time(sec)`))
  Feb13.R2<-Feb13.kg[,c(2,5,8,11)]
  Feb13.R2 <- rename(Feb13.R2, CH2 = CH2.R2, CH3 = CH3.R2 ,CH4 = CH4.R2)
  Feb13.R2 <- Feb13.R2 %>% gather(CH, R2, -c(`Time(sec)`))
  Feb13.tidy<-left_join(Feb13.mO2,Feb13.R2, by = c("Time(sec)", "CH")) 
  #Feb13.tidy<-Feb13.tidy%>%select(-std.time.y)
  Feb13.tidy<-drop_na(Feb13.tidy, "mgO2")
  Feb13.tidy<-filter(Feb13.tidy,R2>.95)
#extract metabolic measures  
  Feb13.SMR<- setDT(Feb13.tidy)[, list(SMR.meanBottom10pct = mean(mgO2[mgO2<=quantile(mgO2, 0.1)])), by=CH]
  Feb13.MMR <- aggregate(mgO2 ~ CH, Feb13.tidy, max)
  Feb13.MMR<-merge(Feb13.MMR,Feb13.tidy)
  Feb13.all<-merge(Feb13.SMR,Feb13.MMR)
  Feb13.all<-rename(Feb13.all,`MMR(mgO2/kg/hr)`=mgO2)
  Feb13.all<-rename(Feb13.all,Time.MMR=`Time(sec)`)
  Feb13.all<-add_column(Feb13.all, date = "Feb13")

```

Jan 27 (background in channel 2)
```{r}
Jan27.kg<-get.witrox.data("27-01-2019_hour_17h.txt", lines.skip = 13,
                      delimit = "tab", choose.names = TRUE,
                      chosen.names = Proj4.background.format,
                      format = "%d-%m-%Y/%H:%M:%S")
    Jan27.temp<-select(Jan27.kg,33)
    Jan27.background<-select(Jan27.kg,c(2,4,12)) #this is specific to background in CH2 (extracting just the necessary columns for background)
    Jan27.background<-Jan27.background %>% separate(`Phase.Time(sec)`, c(NA,NA, "M"),fill = "left") #if it's just M makeing sure that it ends up on the right
    Jan27.background<-Jan27.background %>% separate(M, into = c("Phase", "M.sec"), sep = "(?<=[A-Za-z])(?=[0-9])")
    Jan27.background <- Jan27.background[grep("M", Jan27.background$Phase),] #if there is a phase with just a flush or a wait, make sure it is removed
    Jan27.background$M.sec<-as.numeric(Jan27.background$M.sec)
    Jan27.background<-filter(Jan27.background,M.sec>59) #removes the whole row
    Jan27.background <- na.omit(Jan27.background)
    ggplot(Jan27.background, aes(`Time(sec)`,`CH2.mO2[mgO2/hr]`)) + geom_point()+ geom_smooth(method = "lm") +   ggtitle("Jan27 Background")+ stat_cor(label.x = 2, label.y = .9) + stat_regline_equation(label.x = 2, label.y = 1) + ylim(-.6, 1) #plots the background oxygen consumption
    Jan27.lm<-lm(formula = `CH2.mO2[mgO2/hr]`~ `Time(sec)`, data = Jan27.background) 
    Jan27.background$adjust<-Jan27.lm$fitted.values #add a column of the background values that you are adjusted by to the origional data.frame
    Jan27.background<-select(Jan27.background,c(1,5))
    Jan27.kg <- left_join(Jan27.kg,Jan27.background, by="Time(sec)")
    Jan27.kg<-select(Jan27.kg,-c(3:4,8:11,12:18,22:25,29:32,34:35)) #removing unneeded columns (specifc to which channel has background)
#adjusting for background
  Jan27.kg$`CH1.mO2[mgO2/hr]`<-(Jan27.kg$`CH1.mO2[mgO2/hr]`- Jan27.kg$adjust) 
  Jan27.kg$`CH3.mO2[mgO2/hr]`<-(Jan27.kg$`CH3.mO2[mgO2/hr]`- Jan27.kg$adjust) 
  Jan27.kg$`CH4.mO2[mgO2/hr]`<-(Jan27.kg$`CH4.mO2[mgO2/hr]`- Jan27.kg$adjust)
#adjust for weight (creating new column with these values)
  Jan27.kg$`CH1.mO2[mgO2/kg/hr]`<-(Jan27.kg$`CH1.mO2[mgO2/hr]`/ weight_kg[c("Jan27_Chamber1"),]) 
  Jan27.kg$`CH3.mO2[mgO2/kg/hr]`<-(Jan27.kg$`CH3.mO2[mgO2/hr]`/ weight_kg[c("Jan27_Chamber3"),]) 
  Jan27.kg$`CH4.mO2[mgO2/kg/hr]`<-(Jan27.kg$`CH4.mO2[mgO2/hr]`/ weight_kg[c("Jan27_Chamber4"),])    
#reshape
  Jan27.mO2<-Jan27.kg[,c(2,15,16,17)]
  Jan27.mO2 <- rename(Jan27.mO2, CH1 = `CH1.mO2[mgO2/kg/hr]`, CH3 = `CH3.mO2[mgO2/kg/hr]`,CH4 = `CH4.mO2[mgO2/kg/hr]`)
  Jan27.mO2 <- Jan27.mO2 %>% gather(CH, mgO2, -c(`Time(sec)`))
  Jan27.R2<-Jan27.kg[,c(2,5,8,11)]
  Jan27.R2 <- rename(Jan27.R2, CH1 = CH1.R2, CH3 = CH3.R2 ,CH4 = CH4.R2)
  Jan27.R2 <- Jan27.R2 %>% gather(CH, R2, -c(`Time(sec)`))
  Jan27.tidy<-left_join(Jan27.mO2,Jan27.R2, by = c("Time(sec)", "CH")) 
  #Jan27.tidy<-Jan27.tidy%>%select(-std.time.y)
  Jan27.tidy<-drop_na(Jan27.tidy, "mgO2")
  Jan27.tidy<-filter(Jan27.tidy,R2>.95)
#extract metabolic measures  
  Jan27.SMR<- setDT(Jan27.tidy)[, list(SMR.meanBottom10pct = mean(mgO2[mgO2<=quantile(mgO2, 0.1)])), by=CH]
  Jan27.MMR <- aggregate(mgO2 ~ CH, Jan27.tidy, max)
  Jan27.MMR<-merge(Jan27.MMR,Jan27.tidy)
  Jan27.all<-merge(Jan27.SMR,Jan27.MMR)
  Jan27.all<-rename(Jan27.all,`MMR(mgO2/kg/hr)`=mgO2)
  Jan27.all<-rename(Jan27.all,Time.MMR=`Time(sec)`)
  Jan27.all<-add_column(Jan27.all, date = "Jan27")

```

Jan28 (background in channel 2)
```{r}
#import the data for CH1
Jan28.ch1.start<-get.witrox.data("28-01-2019_MMR_hour.txt", lines.skip = 13,
                      delimit = "tab", choose.names = TRUE,
                      chosen.names = Proj4.background.format,
                      format = "%d-%m-%Y/%H:%M:%S")
Jan28.ch1.start[,12:36]<-NA
Jan28.remainder<-get.witrox.data("28-01-2019_hour.txt", lines.skip = 13,
                      delimit = "tab", choose.names = TRUE,
                      chosen.names = Proj4.background.format,
                      format = "%d-%m-%Y/%H:%M:%S")
Jan28.remainder$`Time(sec)`<-Jan28.remainder[,2]+2427
#background to build the equation
Jan28.background<-select(Jan28.remainder,c(2,4,12))
Jan28.background<-Jan28.background %>% separate(`Phase.Time(sec)`, c(NA,NA, "M"),fill = "left")
Jan28.background<-Jan28.background %>% separate(M, into = c("Phase", "M.sec"), sep = "(?<=[A-Za-z])(?=[0-9])")
Jan28.background<-Jan28.background[grep("M",Jan28.background$Phase),]
Jan28.background$M.sec<-as.numeric(Jan28.background$M.sec)
Jan28.background<-filter(Jan28.background,M.sec>59)
Jan28.background<-na.omit(Jan28.background)
ggplot(Jan28.background, aes(`Time(sec)`,`CH2.mO2[mgO2/hr]`)) + geom_point()+ geom_smooth(method = "lm") +   ggtitle("Jan28 Background")+ stat_cor(label.x = 2, label.y = .9) + stat_regline_equation(label.x = 2, label.y = 1) + ylim(-.6, 1) 
Jan28.lm<-lm(formula = `CH2.mO2[mgO2/hr]`~ `Time(sec)`, data = Jan28.background)
Jan28.background$adjust<-Jan28.lm$fitted.values
#making the other half of the data.frame
Jan28.ch1.background<-select(Jan28.ch1.start,c(2,4,12))
Jan28.ch1.background<-Jan28.ch1.background %>% separate(`Phase.Time(sec)`, c(NA,NA, "M"),fill = "left")
Jan28.ch1.background<-Jan28.ch1.background %>% separate(M, into = c("Phase", "M.sec"), sep = "(?<=[A-Za-z])(?=[0-9])")
Jan28.ch1.background<-Jan28.ch1.background[grep("M", Jan28.ch1.background$Phase),]
Jan28.ch1.background$M.sec<-as.numeric(Jan28.ch1.background$M.sec)
Jan28.ch1.background<-filter(Jan28.ch1.background,M.sec>59)
extrapolate<-select(Jan28.ch1.background,`Time(sec)`)
Jan28.ch1.background$adjust<-predict(Jan28.lm,newdata=extrapolate)
#stack the dataframes
Jan28.all.background<-bind_rows(Jan28.ch1.background,Jan28.background)#background dataframes
Jan28.kg<-bind_rows(Jan28.ch1.start,Jan28.remainder)
Jan28.temp<-select(Jan28.kg,33)
Jan28.temp<-drop_na(Jan28.temp)
Jan28.kg<-left_join(Jan28.kg,Jan28.all.background,by="Time(sec)")
Jan28.kg<-select(Jan28.kg,-c(3,4,8:11,12:18,22:25,29:32,34:35))
#adjusting for background
Jan28.kg$`CH1.mO2[mgO2/hr]`<-(Jan28.kg$`CH1.mO2[mgO2/hr]`- Jan28.kg$adjust) 
Jan28.kg$`CH3.mO2[mgO2/hr]`<-(Jan28.kg$`CH3.mO2[mgO2/hr]`- Jan28.kg$adjust) 
Jan28.kg$`CH4.mO2[mgO2/hr]`<-(Jan28.kg$`CH4.mO2[mgO2/hr]`- Jan28.kg$adjust)
#adjust for weight (creating new column with these values)
Jan28.kg$`CH1.mO2[mgO2/kg/hr]`<-(Jan28.kg$`CH1.mO2[mgO2/hr]`/ weight_kg[c("Jan28_Chamber1"),]) 
Jan28.kg$`CH3.mO2[mgO2/kg/hr]`<-(Jan28.kg$`CH3.mO2[mgO2/hr]`/ weight_kg[c("Jan28_Chamber3"),]) 
Jan28.kg$`CH4.mO2[mgO2/kg/hr]`<-(Jan28.kg$`CH4.mO2[mgO2/hr]`/ weight_kg[c("Jan28_Chamber4"),])  
#reshape
Jan28.mO2<-Jan28.kg[,c(2,18,19,20)]
Jan28.mO2<-rename(Jan28.mO2, CH1 = `CH1.mO2[mgO2/kg/hr]`, CH3 = `CH3.mO2[mgO2/kg/hr]`,CH4 = `CH4.mO2[mgO2/kg/hr]`)
Jan28.mO2<-Jan28.mO2 %>% gather(CH,mgO2,-(`Time(sec)`))
Jan28.mO2<-na.omit(Jan28.mO2)
Jan28.R2<-Jan28.kg[,c(2,5,8,11)]
Jan28.R2<-rename(Jan28.R2,CH1=CH1.R2,CH3=CH3.R2,CH4=CH4.R2)
Jan28.R2<-Jan28.R2 %>% gather(CH,R2,-c(`Time(sec)`))
Jan28.tidy<-left_join(Jan28.mO2,Jan28.R2,by=c("Time(sec)","CH"))
Jan28.tidy<-drop_na(Jan28.tidy,"mgO2")
Jan28.tidy<-filter(Jan28.tidy,R2>.95)
#extract metabolic measures
Jan28.SMR<-setDT(Jan28.tidy)[,list(SMR.meanBottom10pct=mean(mgO2[mgO2<=quantile(mgO2, 0.1)])), by=CH]
Jan28.MMR <- aggregate(mgO2 ~ CH, Jan28.tidy, max)
Jan28.MMR<-merge(Jan28.MMR,Jan28.tidy)
Jan28.all<-merge(Jan28.SMR,Jan28.MMR)
Jan28.all<-rename(Jan28.all,`MMR(mgO2/kg/hr)`=mgO2)
Jan28.all<-rename(Jan28.all,Time.MMR=`Time(sec)`)
Jan28.all<-add_column(Jan28.all,date="Jan28")

```

Jan31 (background in channel 2)
```{r}
Jan31.kg<-get.witrox.data("31-01-2019_hour_17h.txt", lines.skip = 13,
                      delimit = "tab", choose.names = TRUE,
                      chosen.names = Proj4.background.format,
                      format = "%d-%m-%Y/%H:%M:%S")
    Jan31.temp<-select(Jan31.kg,33)
    Jan31.background<-select(Jan31.kg,c(2,4,12)) #this is specific to background in CH2 (extracting just the necessary columns for background)
    Jan31.background<-Jan31.background %>% separate(`Phase.Time(sec)`, c(NA,NA, "M"),fill = "left") #if it's just M makeing sure that it ends up on the right
    Jan31.background<-Jan31.background %>% separate(M, into = c("Phase", "M.sec"), sep = "(?<=[A-Za-z])(?=[0-9])")
    Jan31.background <- Jan31.background[grep("M", Jan31.background$Phase),] #if there is a phase with just a flush or a wait, make sure it is removed
    Jan31.background$M.sec<-as.numeric(Jan31.background$M.sec)
    Jan31.background<-filter(Jan31.background,M.sec>59) #removes the whole row
    Jan31.background <- na.omit(Jan31.background)
    ggplot(Jan31.background, aes(`Time(sec)`,`CH2.mO2[mgO2/hr]`)) + geom_point()+ geom_smooth(method = "lm") +   ggtitle("Jan31 Background")+ stat_cor(label.x = 2, label.y = .9) + stat_regline_equation(label.x = 2, label.y = 1) + ylim(-.6, 1) #plots the background oxygen consumption
    Jan31.lm<-lm(formula = `CH2.mO2[mgO2/hr]`~ `Time(sec)`, data = Jan31.background) 
    Jan31.background$adjust<-Jan31.lm$fitted.values #add a column of the background values that you are adjusted by to the origional data.frame
    Jan31.background<-select(Jan31.background,c(1,5))
    Jan31.kg <- left_join(Jan31.kg,Jan31.background, by="Time(sec)")
    Jan31.kg<-select(Jan31.kg,-c(3:4,8:11,12:18,22:25,29:32,34:35)) #removing unneeded columns (specifc to which channel has background)
#adjusting for background
  Jan31.kg$`CH1.mO2[mgO2/hr]`<-(Jan31.kg$`CH1.mO2[mgO2/hr]`- Jan31.kg$adjust) 
  Jan31.kg$`CH3.mO2[mgO2/hr]`<-(Jan31.kg$`CH3.mO2[mgO2/hr]`- Jan31.kg$adjust) 
  Jan31.kg$`CH4.mO2[mgO2/hr]`<-(Jan31.kg$`CH4.mO2[mgO2/hr]`- Jan31.kg$adjust)
#adjust for weight (creating new column with these values)
  Jan31.kg$`CH1.mO2[mgO2/kg/hr]`<-(Jan31.kg$`CH1.mO2[mgO2/hr]`/ weight_kg[c("Jan31_Chamber1"),]) 
  Jan31.kg$`CH3.mO2[mgO2/kg/hr]`<-(Jan31.kg$`CH3.mO2[mgO2/hr]`/ weight_kg[c("Jan31_Chamber3"),]) 
  Jan31.kg$`CH4.mO2[mgO2/kg/hr]`<-(Jan31.kg$`CH4.mO2[mgO2/hr]`/ weight_kg[c("Jan31_Chamber4"),])    
#reshape
  Jan31.mO2<-Jan31.kg[,c(2,15,16,17)]
  Jan31.mO2 <- rename(Jan31.mO2, CH1 = `CH1.mO2[mgO2/kg/hr]`, CH3 = `CH3.mO2[mgO2/kg/hr]`,CH4 = `CH4.mO2[mgO2/kg/hr]`)
  Jan31.mO2 <- Jan31.mO2 %>% gather(CH, mgO2, -c(`Time(sec)`))
  Jan31.R2<-Jan31.kg[,c(2,5,8,11)]
  Jan31.R2 <- rename(Jan31.R2, CH1 = CH1.R2, CH3 = CH3.R2 ,CH4 = CH4.R2)
  Jan31.R2 <- Jan31.R2 %>% gather(CH, R2, -c(`Time(sec)`))
  Jan31.tidy<-left_join(Jan31.mO2,Jan31.R2, by = c("Time(sec)", "CH")) 
  #Jan31.tidy<-Jan31.tidy%>%select(-std.time.y)
  Jan31.tidy<-drop_na(Jan31.tidy, "mgO2")
  Jan31.tidy<-filter(Jan31.tidy,R2>.95)
#extract metabolic measures  
  Jan31.SMR<- setDT(Jan31.tidy)[, list(SMR.meanBottom10pct = mean(mgO2[mgO2<=quantile(mgO2, 0.1)])), by=CH]
  Jan31.MMR <- aggregate(mgO2 ~ CH, Jan31.tidy, max)
  Jan31.MMR<-merge(Jan31.MMR,Jan31.tidy)
  Jan31.all<-merge(Jan31.SMR,Jan31.MMR)
  Jan31.all<-rename(Jan31.all,`MMR(mgO2/kg/hr)`=mgO2)
  Jan31.all<-rename(Jan31.all,Time.MMR=`Time(sec)`)
  Jan31.all<-add_column(Jan31.all, date = "Jan31")

```

Feb03 with background in channel 2
```{r}
Feb03.kg<-get.witrox.data("03-02-2019_hour_17h.txt", lines.skip = 13,
                      delimit = "tab", choose.names = TRUE,
                      chosen.names = Proj4.background.format,
                      format = "%d-%m-%Y/%H:%M:%S")
    Feb03.temp<-select(Feb03.kg,33)
    Feb03.background<-select(Feb03.kg,c(2,4,12)) #this is specific to background in CH2 (extracting just the necessary columns for background)
    Feb03.background<-Feb03.background %>% separate(`Phase.Time(sec)`, c(NA,NA, "M"),fill = "left") #if it's just M makeing sure that it ends up on the right
    Feb03.background<-Feb03.background %>% separate(M, into = c("Phase", "M.sec"), sep = "(?<=[A-Za-z])(?=[0-9])")
    Feb03.background <- Feb03.background[grep("M", Feb03.background$Phase),] #if there is a phase with just a flush or a wait, make sure it is removed
    Feb03.background$M.sec<-as.numeric(Feb03.background$M.sec)
    Feb03.background<-filter(Feb03.background,M.sec>59) #removes the whole row
    Feb03.background <- na.omit(Feb03.background)
    ggplot(Feb03.background, aes(`Time(sec)`,`CH2.mO2[mgO2/hr]`)) + geom_point()+ geom_smooth(method = "lm") +   ggtitle("Feb03 Background")+ stat_cor(label.x = 2, label.y = .9) + stat_regline_equation(label.x = 2, label.y = 1) + ylim(-.6, 1) #plots the background oxygen consumption
    Feb03.lm<-lm(formula = `CH2.mO2[mgO2/hr]`~ `Time(sec)`, data = Feb03.background) 
    Feb03.background$adjust<-Feb03.lm$fitted.values #add a column of the background values that you are adjusted by to the origional data.frame
    Feb03.background<-select(Feb03.background,c(1,5))
    Feb03.kg <- left_join(Feb03.kg,Feb03.background, by="Time(sec)")
    Feb03.kg<-select(Feb03.kg,-c(3:4,8:11,12:18,22:25,29:32,34:35)) #removing unneeded columns (specifc to which channel has background)
#adjusting for background
  Feb03.kg$`CH1.mO2[mgO2/hr]`<-(Feb03.kg$`CH1.mO2[mgO2/hr]`- Feb03.kg$adjust) 
  Feb03.kg$`CH3.mO2[mgO2/hr]`<-(Feb03.kg$`CH3.mO2[mgO2/hr]`- Feb03.kg$adjust) 
  Feb03.kg$`CH4.mO2[mgO2/hr]`<-(Feb03.kg$`CH4.mO2[mgO2/hr]`- Feb03.kg$adjust)
#adjust for weight (creating new column with these values)
  Feb03.kg$`CH1.mO2[mgO2/kg/hr]`<-(Feb03.kg$`CH1.mO2[mgO2/hr]`/ weight_kg[c("Feb03_Chamber1"),]) 
  Feb03.kg$`CH3.mO2[mgO2/kg/hr]`<-(Feb03.kg$`CH3.mO2[mgO2/hr]`/ weight_kg[c("Feb03_Chamber3"),]) 
  Feb03.kg$`CH4.mO2[mgO2/kg/hr]`<-(Feb03.kg$`CH4.mO2[mgO2/hr]`/ weight_kg[c("Feb03_Chamber4"),])    
#reshape
  Feb03.mO2<-Feb03.kg[,c(2,15,16,17)]
  Feb03.mO2 <- rename(Feb03.mO2, CH1 = `CH1.mO2[mgO2/kg/hr]`, CH3 = `CH3.mO2[mgO2/kg/hr]`,CH4 = `CH4.mO2[mgO2/kg/hr]`)
  Feb03.mO2 <- Feb03.mO2 %>% gather(CH, mgO2, -c(`Time(sec)`))
  Feb03.R2<-Feb03.kg[,c(2,5,8,11)]
  Feb03.R2 <- rename(Feb03.R2, CH1 = CH1.R2, CH3 = CH3.R2 ,CH4 = CH4.R2)
  Feb03.R2 <- Feb03.R2 %>% gather(CH, R2, -c(`Time(sec)`))
  Feb03.tidy<-left_join(Feb03.mO2,Feb03.R2, by = c("Time(sec)", "CH")) 
  #Feb03.tidy<-Feb03.tidy%>%select(-std.time.y)
  Feb03.tidy<-drop_na(Feb03.tidy, "mgO2")
  Feb03.tidy<-filter(Feb03.tidy,R2>.95)
#extract metabolic measures  
  Feb03.SMR<- setDT(Feb03.tidy)[, list(SMR.meanBottom10pct = mean(mgO2[mgO2<=quantile(mgO2, 0.1)])), by=CH]
  Feb03.MMR <- aggregate(mgO2 ~ CH, Feb03.tidy, max)
  Feb03.MMR<-merge(Feb03.MMR,Feb03.tidy)
  Feb03.all<-merge(Feb03.SMR,Feb03.MMR)
  Feb03.all<-rename(Feb03.all,`MMR(mgO2/kg/hr)`=mgO2)
  Feb03.all<-rename(Feb03.all,Time.MMR=`Time(sec)`)
  Feb03.all<-add_column(Feb03.all, date = "Feb03")


```

Fe04 with background in channel 2
```{r}
Feb04.kg<-get.witrox.data("04-02-2019_hour_17h.txt", lines.skip = 13,
                      delimit = "tab", choose.names = TRUE,
                      chosen.names = Proj4.background.format,
                      format = "%d-%m-%Y/%H:%M:%S")
    Feb04.temp<-select(Feb04.kg,33)
    Feb04.background<-select(Feb04.kg,c(2,4,12)) #this is specific to background in CH2 (extracting just the necessary columns for background)
    Feb04.background<-Feb04.background %>% separate(`Phase.Time(sec)`, c(NA,NA, "M"),fill = "left") #if it's just M makeing sure that it ends up on the right
    Feb04.background<-Feb04.background %>% separate(M, into = c("Phase", "M.sec"), sep = "(?<=[A-Za-z])(?=[0-9])")
    Feb04.background <- Feb04.background[grep("M", Feb04.background$Phase),] #if there is a phase with just a flush or a wait, make sure it is removed
    Feb04.background$M.sec<-as.numeric(Feb04.background$M.sec)
    Feb04.background<-filter(Feb04.background,M.sec>59) #removes the whole row
    Feb04.background <- na.omit(Feb04.background)
    ggplot(Feb04.background, aes(`Time(sec)`,`CH2.mO2[mgO2/hr]`)) + geom_point()+ geom_smooth(method = "lm") +   ggtitle("Feb04 Background")+ stat_cor(label.x = 2, label.y = .9) + stat_regline_equation(label.x = 2, label.y = 1) + ylim(-.6, 1) #plots the background oxygen consumption
    Feb04.lm<-lm(formula = `CH2.mO2[mgO2/hr]`~ `Time(sec)`, data = Feb04.background) 
    Feb04.background$adjust<-Feb04.lm$fitted.values #add a column of the background values that you are adjusted by to the origional data.frame
    Feb04.background<-select(Feb04.background,c(1,5))
    Feb04.kg <- left_join(Feb04.kg,Feb04.background, by="Time(sec)")
    Feb04.kg<-select(Feb04.kg,-c(3:4,8:11,12:18,22:25,29:32,34:35)) #removing unneeded columns (specifc to which channel has background)
#adjusting for background
  Feb04.kg$`CH1.mO2[mgO2/hr]`<-(Feb04.kg$`CH1.mO2[mgO2/hr]`- Feb04.kg$adjust) 
  Feb04.kg$`CH3.mO2[mgO2/hr]`<-(Feb04.kg$`CH3.mO2[mgO2/hr]`- Feb04.kg$adjust) 
  Feb04.kg$`CH4.mO2[mgO2/hr]`<-(Feb04.kg$`CH4.mO2[mgO2/hr]`- Feb04.kg$adjust)
#adjust for weight (creating new column with these values)
  Feb04.kg$`CH1.mO2[mgO2/kg/hr]`<-(Feb04.kg$`CH1.mO2[mgO2/hr]`/ weight_kg[c("Feb04_Chamber1"),]) 
  Feb04.kg$`CH3.mO2[mgO2/kg/hr]`<-(Feb04.kg$`CH3.mO2[mgO2/hr]`/ weight_kg[c("Feb04_Chamber3"),]) 
  Feb04.kg$`CH4.mO2[mgO2/kg/hr]`<-(Feb04.kg$`CH4.mO2[mgO2/hr]`/ weight_kg[c("Feb04_Chamber4"),])    
#reshape
  Feb04.mO2<-Feb04.kg[,c(2,15,16,17)]
  Feb04.mO2 <- rename(Feb04.mO2, CH1 = `CH1.mO2[mgO2/kg/hr]`, CH3 = `CH3.mO2[mgO2/kg/hr]`,CH4 = `CH4.mO2[mgO2/kg/hr]`)
  Feb04.mO2 <- Feb04.mO2 %>% gather(CH, mgO2, -c(`Time(sec)`))
  Feb04.R2<-Feb04.kg[,c(2,5,8,11)]
  Feb04.R2 <- rename(Feb04.R2, CH1 = CH1.R2, CH3 = CH3.R2 ,CH4 = CH4.R2)
  Feb04.R2 <- Feb04.R2 %>% gather(CH, R2, -c(`Time(sec)`))
  Feb04.tidy<-left_join(Feb04.mO2,Feb04.R2, by = c("Time(sec)", "CH")) 
  #Feb04.tidy<-Feb04.tidy%>%select(-std.time.y)
  Feb04.tidy<-drop_na(Feb04.tidy, "mgO2")
  Feb04.tidy<-filter(Feb04.tidy,R2>.95)
#extract metabolic measures  
  Feb04.SMR<- setDT(Feb04.tidy)[, list(SMR.meanBottom10pct = mean(mgO2[mgO2<=quantile(mgO2, 0.1)])), by=CH]
  Feb04.MMR <- aggregate(mgO2 ~ CH, Feb04.tidy, max)
  Feb04.MMR<-merge(Feb04.MMR,Feb04.tidy)
  Feb04.all<-merge(Feb04.SMR,Feb04.MMR)
  Feb04.all<-rename(Feb04.all,`MMR(mgO2/kg/hr)`=mgO2)
  Feb04.all<-rename(Feb04.all,Time.MMR=`Time(sec)`)
  Feb04.all<-add_column(Feb04.all, date = "Feb04")

```

Feb07 with background in channel 2
```{r}
Feb07.kg<-get.witrox.data("07-02-2019_hour_17h.txt", lines.skip = 13,
                      delimit = "tab", choose.names = TRUE,
                      chosen.names = Proj4.background.format,
                      format = "%d-%m-%Y/%H:%M:%S")
    Feb07.temp<-select(Feb07.kg,33)
    Feb07.background<-select(Feb07.kg,c(2,4,12)) #this is specific to background in CH2 (extracting just the necessary columns for background)
    Feb07.background<-Feb07.background %>% separate(`Phase.Time(sec)`, c(NA,NA, "M"),fill = "left") #if it's just M makeing sure that it ends up on the right
    Feb07.background<-Feb07.background %>% separate(M, into = c("Phase", "M.sec"), sep = "(?<=[A-Za-z])(?=[0-9])")
    Feb07.background <- Feb07.background[grep("M", Feb07.background$Phase),] #if there is a phase with just a flush or a wait, make sure it is removed
    Feb07.background$M.sec<-as.numeric(Feb07.background$M.sec)
    Feb07.background<-filter(Feb07.background,M.sec>59) #removes the whole row
    Feb07.background <- na.omit(Feb07.background)
    ggplot(Feb07.background, aes(`Time(sec)`,`CH2.mO2[mgO2/hr]`)) + geom_point()+ geom_smooth(method = "lm") +   ggtitle("Feb07 Background")+ stat_cor(label.x = 2, label.y = .9) + stat_regline_equation(label.x = 2, label.y = 1) + ylim(-.6, 1) #plots the background oxygen consumption
    Feb07.lm<-lm(formula = `CH2.mO2[mgO2/hr]`~ `Time(sec)`, data = Feb07.background) 
    Feb07.background$adjust<-Feb07.lm$fitted.values #add a column of the background values that you are adjusted by to the origional data.frame
    Feb07.background<-select(Feb07.background,c(1,5))
    Feb07.kg <- left_join(Feb07.kg,Feb07.background, by="Time(sec)")
    Feb07.kg<-select(Feb07.kg,-c(3:4,8:11,12:18,22:25,29:32,34:35)) #removing unneeded columns (specifc to which channel has background)
#adjusting for background
  Feb07.kg$`CH1.mO2[mgO2/hr]`<-(Feb07.kg$`CH1.mO2[mgO2/hr]`- Feb07.kg$adjust) 
  Feb07.kg$`CH3.mO2[mgO2/hr]`<-(Feb07.kg$`CH3.mO2[mgO2/hr]`- Feb07.kg$adjust) 
  Feb07.kg$`CH4.mO2[mgO2/hr]`<-(Feb07.kg$`CH4.mO2[mgO2/hr]`- Feb07.kg$adjust)
#adjust for weight (creating new column with these values)
  Feb07.kg$`CH1.mO2[mgO2/kg/hr]`<-(Feb07.kg$`CH1.mO2[mgO2/hr]`/ weight_kg[c("Feb07_Chamber1"),]) 
  Feb07.kg$`CH3.mO2[mgO2/kg/hr]`<-(Feb07.kg$`CH3.mO2[mgO2/hr]`/ weight_kg[c("Feb07_Chamber3"),]) 
  Feb07.kg$`CH4.mO2[mgO2/kg/hr]`<-(Feb07.kg$`CH4.mO2[mgO2/hr]`/ weight_kg[c("Feb07_Chamber4"),])    
#reshape
  Feb07.mO2<-Feb07.kg[,c(2,15,16,17)]
  Feb07.mO2 <- rename(Feb07.mO2, CH1 = `CH1.mO2[mgO2/kg/hr]`, CH3 = `CH3.mO2[mgO2/kg/hr]`,CH4 = `CH4.mO2[mgO2/kg/hr]`)
  Feb07.mO2 <- Feb07.mO2 %>% gather(CH, mgO2, -c(`Time(sec)`))
  Feb07.R2<-Feb07.kg[,c(2,5,8,11)]
  Feb07.R2 <- rename(Feb07.R2, CH1 = CH1.R2, CH3 = CH3.R2 ,CH4 = CH4.R2)
  Feb07.R2 <- Feb07.R2 %>% gather(CH, R2, -c(`Time(sec)`))
  Feb07.tidy<-left_join(Feb07.mO2,Feb07.R2, by = c("Time(sec)", "CH")) 
  #Feb07.tidy<-Feb07.tidy%>%select(-std.time.y)
  Feb07.tidy<-drop_na(Feb07.tidy, "mgO2")
  Feb07.tidy<-filter(Feb07.tidy,R2>.95)
#extract metabolic measures  
  Feb07.SMR<- setDT(Feb07.tidy)[, list(SMR.meanBottom10pct = mean(mgO2[mgO2<=quantile(mgO2, 0.1)])), by=CH]
  Feb07.MMR <- aggregate(mgO2 ~ CH, Feb07.tidy, max)
  Feb07.MMR<-merge(Feb07.MMR,Feb07.tidy)
  Feb07.all<-merge(Feb07.SMR,Feb07.MMR)
  Feb07.all<-rename(Feb07.all,`MMR(mgO2/kg/hr)`=mgO2)
  Feb07.all<-rename(Feb07.all,Time.MMR=`Time(sec)`)
  Feb07.all<-add_column(Feb07.all, date = "Feb07")

```

Feb11 (with background in channel 2)
```{r}
Feb11.kg<-get.witrox.data("11-02-2019_hour_17h.txt", lines.skip = 13,
                      delimit = "tab", choose.names = TRUE,
                      chosen.names = Proj4.background.format,
                      format = "%d-%m-%Y/%H:%M:%S")
    Feb11.temp<-select(Feb11.kg,33)
    Feb11.background<-select(Feb11.kg,c(2,4,12)) #this is specific to background in CH2 (extracting just the necessary columns for background)
    Feb11.background<-Feb11.background %>% separate(`Phase.Time(sec)`, c(NA,NA, "M"),fill = "left") #if it's just M makeing sure that it ends up on the right
    Feb11.background<-Feb11.background %>% separate(M, into = c("Phase", "M.sec"), sep = "(?<=[A-Za-z])(?=[0-9])")
    Feb11.background <- Feb11.background[grep("M", Feb11.background$Phase),] #if there is a phase with just a flush or a wait, make sure it is removed
    Feb11.background$M.sec<-as.numeric(Feb11.background$M.sec)
    Feb11.background<-filter(Feb11.background,M.sec>59) #removes the whole row
    Feb11.background <- na.omit(Feb11.background)
    ggplot(Feb11.background, aes(`Time(sec)`,`CH2.mO2[mgO2/hr]`)) + geom_point()+ geom_smooth(method = "lm") +   ggtitle("Feb11 Background")+ stat_cor(label.x = 2, label.y = .9) + stat_regline_equation(label.x = 2, label.y = 1) + ylim(-.6, 1) #plots the background oxygen consumption
    Feb11.lm<-lm(formula = `CH2.mO2[mgO2/hr]`~ `Time(sec)`, data = Feb11.background) 
    Feb11.background$adjust<-Feb11.lm$fitted.values #add a column of the background values that you are adjusted by to the origional data.frame
    Feb11.background<-select(Feb11.background,c(1,5))
    Feb11.kg <- left_join(Feb11.kg,Feb11.background, by="Time(sec)")
    Feb11.kg<-select(Feb11.kg,-c(3:4,8:11,12:18,22:25,29:32,34:35)) #removing unneeded columns (specifc to which channel has background)
#adjusting for background
  Feb11.kg$`CH1.mO2[mgO2/hr]`<-(Feb11.kg$`CH1.mO2[mgO2/hr]`- Feb11.kg$adjust) 
  Feb11.kg$`CH3.mO2[mgO2/hr]`<-(Feb11.kg$`CH3.mO2[mgO2/hr]`- Feb11.kg$adjust) 
  Feb11.kg$`CH4.mO2[mgO2/hr]`<-(Feb11.kg$`CH4.mO2[mgO2/hr]`- Feb11.kg$adjust)
#adjust for weight (creating new column with these values)
  Feb11.kg$`CH1.mO2[mgO2/kg/hr]`<-(Feb11.kg$`CH1.mO2[mgO2/hr]`/ weight_kg[c("Feb11_Chamber1"),]) 
  Feb11.kg$`CH3.mO2[mgO2/kg/hr]`<-(Feb11.kg$`CH3.mO2[mgO2/hr]`/ weight_kg[c("Feb11_Chamber3"),]) 
  Feb11.kg$`CH4.mO2[mgO2/kg/hr]`<-(Feb11.kg$`CH4.mO2[mgO2/hr]`/ weight_kg[c("Feb11_Chamber4"),])    
#reshape
  Feb11.mO2<-Feb11.kg[,c(2,15,16,17)]
  Feb11.mO2 <- rename(Feb11.mO2, CH1 = `CH1.mO2[mgO2/kg/hr]`, CH3 = `CH3.mO2[mgO2/kg/hr]`,CH4 = `CH4.mO2[mgO2/kg/hr]`)
  Feb11.mO2 <- Feb11.mO2 %>% gather(CH, mgO2, -c(`Time(sec)`))
  Feb11.R2<-Feb11.kg[,c(2,5,8,11)]
  Feb11.R2 <- rename(Feb11.R2, CH1 = CH1.R2, CH3 = CH3.R2 ,CH4 = CH4.R2)
  Feb11.R2 <- Feb11.R2 %>% gather(CH, R2, -c(`Time(sec)`))
  Feb11.tidy<-left_join(Feb11.mO2,Feb11.R2, by = c("Time(sec)", "CH")) 
  #Feb11.tidy<-Feb11.tidy%>%select(-std.time.y)
  Feb11.tidy<-drop_na(Feb11.tidy, "mgO2")
  Feb11.tidy<-filter(Feb11.tidy,R2>.95)
#extract metabolic measures  
  Feb11.SMR<- setDT(Feb11.tidy)[, list(SMR.meanBottom10pct = mean(mgO2[mgO2<=quantile(mgO2, 0.1)])), by=CH]
  Feb11.MMR <- aggregate(mgO2 ~ CH, Feb11.tidy, max)
  Feb11.MMR<-merge(Feb11.MMR,Feb11.tidy)
  Feb11.all<-merge(Feb11.SMR,Feb11.MMR)
  Feb11.all<-rename(Feb11.all,`MMR(mgO2/kg/hr)`=mgO2)
  Feb11.all<-rename(Feb11.all,Time.MMR=`Time(sec)`)
  Feb11.all<-add_column(Feb11.all, date = "Feb11")

```

Feb12 with background in channel 2
```{r}
Feb12.kg<-get.witrox.data("12-02-2019_hour_17h.txt", lines.skip = 13,
                      delimit = "tab", choose.names = TRUE,
                      chosen.names = Proj4.background.format,
                      format = "%d-%m-%Y/%H:%M:%S")
    Feb12.temp<-select(Feb12.kg,33)
    Feb12.background<-select(Feb12.kg,c(2,4,12)) #this is specific to background in CH2 (extracting just the necessary columns for background)
    Feb12.background<-Feb12.background %>% separate(`Phase.Time(sec)`, c(NA,NA, "M"),fill = "left") #if it's just M makeing sure that it ends up on the right
    Feb12.background<-Feb12.background %>% separate(M, into = c("Phase", "M.sec"), sep = "(?<=[A-Za-z])(?=[0-9])")
    Feb12.background <- Feb12.background[grep("M", Feb12.background$Phase),] #if there is a phase with just a flush or a wait, make sure it is removed
    Feb12.background$M.sec<-as.numeric(Feb12.background$M.sec)
    Feb12.background<-filter(Feb12.background,M.sec>59) #removes the whole row
    Feb12.background <- na.omit(Feb12.background)
    ggplot(Feb12.background, aes(`Time(sec)`,`CH2.mO2[mgO2/hr]`)) + geom_point()+ geom_smooth(method = "lm") +   ggtitle("Feb12 Background")+ stat_cor(label.x = 2, label.y = .9) + stat_regline_equation(label.x = 2, label.y = 1) + ylim(-.6, 1) #plots the background oxygen consumption
    Feb12.lm<-lm(formula = `CH2.mO2[mgO2/hr]`~ `Time(sec)`, data = Feb12.background) 
    Feb12.background$adjust<-Feb12.lm$fitted.values #add a column of the background values that you are adjusted by to the origional data.frame
    Feb12.background<-select(Feb12.background,c(1,5))
    Feb12.kg <- left_join(Feb12.kg,Feb12.background, by="Time(sec)")
    Feb12.kg<-select(Feb12.kg,-c(3:4,8:11,12:18,22:25,29:32,34:35)) #removing unneeded columns (specifc to which channel has background)
#adjusting for background
  Feb12.kg$`CH1.mO2[mgO2/hr]`<-(Feb12.kg$`CH1.mO2[mgO2/hr]`- Feb12.kg$adjust) 
  Feb12.kg$`CH3.mO2[mgO2/hr]`<-(Feb12.kg$`CH3.mO2[mgO2/hr]`- Feb12.kg$adjust) 
  Feb12.kg$`CH4.mO2[mgO2/hr]`<-(Feb12.kg$`CH4.mO2[mgO2/hr]`- Feb12.kg$adjust)
#adjust for weight (creating new column with these values)
  Feb12.kg$`CH1.mO2[mgO2/kg/hr]`<-(Feb12.kg$`CH1.mO2[mgO2/hr]`/ weight_kg[c("Feb12_Chamber1"),]) 
  Feb12.kg$`CH3.mO2[mgO2/kg/hr]`<-(Feb12.kg$`CH3.mO2[mgO2/hr]`/ weight_kg[c("Feb12_Chamber3"),]) 
  Feb12.kg$`CH4.mO2[mgO2/kg/hr]`<-(Feb12.kg$`CH4.mO2[mgO2/hr]`/ weight_kg[c("Feb12_Chamber4"),])    
#reshape
  Feb12.mO2<-Feb12.kg[,c(2,15,16,17)]
  Feb12.mO2 <- rename(Feb12.mO2, CH1 = `CH1.mO2[mgO2/kg/hr]`, CH3 = `CH3.mO2[mgO2/kg/hr]`,CH4 = `CH4.mO2[mgO2/kg/hr]`)
  Feb12.mO2 <- Feb12.mO2 %>% gather(CH, mgO2, -c(`Time(sec)`))
  Feb12.R2<-Feb12.kg[,c(2,5,8,11)]
  Feb12.R2 <- rename(Feb12.R2, CH1 = CH1.R2, CH3 = CH3.R2 ,CH4 = CH4.R2)
  Feb12.R2 <- Feb12.R2 %>% gather(CH, R2, -c(`Time(sec)`))
  Feb12.tidy<-left_join(Feb12.mO2,Feb12.R2, by = c("Time(sec)", "CH")) 
  #Feb12.tidy<-Feb12.tidy%>%select(-std.time.y)
  Feb12.tidy<-drop_na(Feb12.tidy, "mgO2")
  Feb12.tidy<-filter(Feb12.tidy,R2>.95)
#extract metabolic measures  
  Feb12.SMR<- setDT(Feb12.tidy)[, list(SMR.meanBottom10pct = mean(mgO2[mgO2<=quantile(mgO2, 0.1)])), by=CH]
  Feb12.MMR <- aggregate(mgO2 ~ CH, Feb12.tidy, max)
  Feb12.MMR<-merge(Feb12.MMR,Feb12.tidy)
  Feb12.all<-merge(Feb12.SMR,Feb12.MMR)
  Feb12.all<-rename(Feb12.all,`MMR(mgO2/kg/hr)`=mgO2)
  Feb12.all<-rename(Feb12.all,Time.MMR=`Time(sec)`)
  Feb12.all<-add_column(Feb12.all, date = "Feb12")

```

Feb 15th with background in channel 2
```{r}
Feb15.kg<-get.witrox.data("15-02-2019_hour_17h.txt", lines.skip = 13,
                      delimit = "tab", choose.names = TRUE,
                      chosen.names = Proj4.background.format,
                      format = "%d-%m-%Y/%H:%M:%S")
    Feb15.temp<-select(Feb15.kg,33)
    Feb15.background<-select(Feb15.kg,c(2,4,12)) #this is specific to background in CH2 (extracting just the necessary columns for background)
    Feb15.background<-Feb15.background %>% separate(`Phase.Time(sec)`, c(NA,NA, "M"),fill = "left") #if it's just M makeing sure that it ends up on the right
    Feb15.background<-Feb15.background %>% separate(M, into = c("Phase", "M.sec"), sep = "(?<=[A-Za-z])(?=[0-9])")
    Feb15.background <- Feb15.background[grep("M", Feb15.background$Phase),] #if there is a phase with just a flush or a wait, make sure it is removed
    Feb15.background$M.sec<-as.numeric(Feb15.background$M.sec)
    Feb15.background<-filter(Feb15.background,M.sec>59) #removes the whole row
    Feb15.background <- na.omit(Feb15.background)
    ggplot(Feb15.background, aes(`Time(sec)`,`CH2.mO2[mgO2/hr]`)) + geom_point()+ geom_smooth(method = "lm") +   ggtitle("Feb15 Background")+ stat_cor(label.x = 2, label.y = .9) + stat_regline_equation(label.x = 2, label.y = 1) + ylim(-.6, 1) #plots the background oxygen consumption
    Feb15.lm<-lm(formula = `CH2.mO2[mgO2/hr]`~ `Time(sec)`, data = Feb15.background) 
    Feb15.background$adjust<-Feb15.lm$fitted.values #add a column of the background values that you are adjusted by to the origional data.frame
    Feb15.background<-select(Feb15.background,c(1,5))
    Feb15.kg <- left_join(Feb15.kg,Feb15.background, by="Time(sec)")
    Feb15.kg<-select(Feb15.kg,-c(3:4,8:11,12:18,22:25,29:32,34:35)) #removing unneeded columns (specifc to which channel has background)
#adjusting for background
  Feb15.kg$`CH1.mO2[mgO2/hr]`<-(Feb15.kg$`CH1.mO2[mgO2/hr]`- Feb15.kg$adjust) 
  Feb15.kg$`CH3.mO2[mgO2/hr]`<-(Feb15.kg$`CH3.mO2[mgO2/hr]`- Feb15.kg$adjust) 
  Feb15.kg$`CH4.mO2[mgO2/hr]`<-(Feb15.kg$`CH4.mO2[mgO2/hr]`- Feb15.kg$adjust)
#adjust for weight (creating new column with these values)
  Feb15.kg$`CH1.mO2[mgO2/kg/hr]`<-(Feb15.kg$`CH1.mO2[mgO2/hr]`/ weight_kg[c("Feb15_Chamber1"),]) 
  Feb15.kg$`CH3.mO2[mgO2/kg/hr]`<-(Feb15.kg$`CH3.mO2[mgO2/hr]`/ weight_kg[c("Feb15_Chamber3"),]) 
  Feb15.kg$`CH4.mO2[mgO2/kg/hr]`<-(Feb15.kg$`CH4.mO2[mgO2/hr]`/ weight_kg[c("Feb15_Chamber4"),])    
#reshape
  Feb15.mO2<-Feb15.kg[,c(2,15,16,17)]
  Feb15.mO2 <- rename(Feb15.mO2, CH1 = `CH1.mO2[mgO2/kg/hr]`, CH3 = `CH3.mO2[mgO2/kg/hr]`,CH4 = `CH4.mO2[mgO2/kg/hr]`)
  Feb15.mO2 <- Feb15.mO2 %>% gather(CH, mgO2, -c(`Time(sec)`))
  Feb15.R2<-Feb15.kg[,c(2,5,8,11)]
  Feb15.R2 <- rename(Feb15.R2, CH1 = CH1.R2, CH3 = CH3.R2 ,CH4 = CH4.R2)
  Feb15.R2 <- Feb15.R2 %>% gather(CH, R2, -c(`Time(sec)`))
  Feb15.tidy<-left_join(Feb15.mO2,Feb15.R2, by = c("Time(sec)", "CH")) 
  #Feb15.tidy<-Feb15.tidy%>%select(-std.time.y)
  Feb15.tidy<-drop_na(Feb15.tidy, "mgO2")
  Feb15.tidy<-filter(Feb15.tidy,R2>.95)
#extract metabolic measures  
  Feb15.SMR<- setDT(Feb15.tidy)[, list(SMR.meanBottom10pct = mean(mgO2[mgO2<=quantile(mgO2, 0.1)])), by=CH]
  Feb15.MMR <- aggregate(mgO2 ~ CH, Feb15.tidy, max)
  Feb15.MMR<-merge(Feb15.MMR,Feb15.tidy)
  Feb15.all<-merge(Feb15.SMR,Feb15.MMR)
  Feb15.all<-rename(Feb15.all,`MMR(mgO2/kg/hr)`=mgO2)
  Feb15.all<-rename(Feb15.all,Time.MMR=`Time(sec)`)
  Feb15.all<-add_column(Feb15.all, date = "Feb15")

```

Jan30th with background in channel 3
```{r}
Jan30.kg<-get.witrox.data("30-01-2019_hour_17h.txt", lines.skip = 13,
                      delimit = "tab", choose.names = TRUE,
                      chosen.names = Proj4.background.format,
                      format = "%d-%m-%Y/%H:%M:%S")
    Jan30.temp<-select(Jan30.kg,33)
    Jan30.background<-select(Jan30.kg,c(2,4,19)) #this is specific to background in CH1 (extracting just the necessary columns for background)
    Jan30.background<-Jan30.background %>% separate(`Phase.Time(sec)`, c(NA,NA, "M"),fill = "left") #if it's just M makeing sure that it ends up on the right
    Jan30.background<-Jan30.background %>% separate(M, into = c("Phase", "M.sec"), sep = "(?<=[A-Za-z])(?=[0-9])")
    Jan30.background <- Jan30.background[grep("M", Jan30.background$Phase),] #if there is a phase with just a flush or a wait, make sure it is removed
    Jan30.background$M.sec<-as.numeric(Jan30.background$M.sec)
    Jan30.background<-filter(Jan30.background,M.sec>59) #removes the whole row
    Jan30.background <- na.omit(Jan30.background)
    ggplot(Jan30.background, aes(`Time(sec)`,`CH3.mO2[mgO2/hr]`)) + geom_point()+ geom_smooth(method = "lm") +   ggtitle("Jan30 Background")+ stat_cor(label.x = 2, label.y = .9) + stat_regline_equation(label.x = 2, label.y = 1) + ylim(-.6, 1) #plots the background oxygen consumption
    Jan30.lm<-lm(formula = `CH3.mO2[mgO2/hr]`~ `Time(sec)`, data = Jan30.background) 
    Jan30.background$adjust<-Jan30.lm$fitted.values #add a column of the background values that you are adjusted by to the origional data.frame
    Jan30.background<-select(Jan30.background,c(1,5))
    Jan30.kg <- left_join(Jan30.kg,Jan30.background, by="Time(sec)")
    Jan30.kg<-select(Jan30.kg,-c(3:4,8:11,15:18,19:25,29:32,34:35)) #removing unneeded columns (specifc to which channel has background)
#adjusting for background (specific to which channel has background)
  Jan30.kg$`CH1.mO2[mgO2/hr]`<-(Jan30.kg$`CH1.mO2[mgO2/hr]`- Jan30.kg$adjust) 
  Jan30.kg$`CH2.mO2[mgO2/hr]`<-(Jan30.kg$`CH2.mO2[mgO2/hr]`- Jan30.kg$adjust) 
  Jan30.kg$`CH4.mO2[mgO2/hr]`<-(Jan30.kg$`CH4.mO2[mgO2/hr]`- Jan30.kg$adjust)
#adjust for weight (creating new column with these values) (specific to which channel has background)
  Jan30.kg$`CH1.mO2[mgO2/kg/hr]`<-(Jan30.kg$`CH1.mO2[mgO2/hr]`/ weight_kg[c("Jan30_Chamber1"),]) 
  Jan30.kg$`CH2.mO2[mgO2/kg/hr]`<-(Jan30.kg$`CH2.mO2[mgO2/hr]`/ weight_kg[c("Jan30_Chamber2"),]) 
  Jan30.kg$`CH4.mO2[mgO2/kg/hr]`<-(Jan30.kg$`CH4.mO2[mgO2/hr]`/ weight_kg[c("Jan30_Chamber4"),])    
#reshape
  Jan30.mO2<-Jan30.kg[,c(2,15,16,17)] #not specific to different background channel, the columns extracted stay the same
  Jan30.mO2 <- rename(Jan30.mO2, CH1 = `CH1.mO2[mgO2/kg/hr]`, CH2 = `CH2.mO2[mgO2/kg/hr]`,CH4 = `CH4.mO2[mgO2/kg/hr]`) 
  Jan30.mO2 <- Jan30.mO2 %>% gather (CH, mgO2, -c(`Time(sec)`))
  Jan30.R2<-Jan30.kg[,c(2,5,8,11)] #not specific to different background channel the extracted columns stay the same
  Jan30.R2 <- rename(Jan30.R2, CH1 = CH1.R2, CH2 = CH2.R2 ,CH4 = CH4.R2)
  Jan30.R2 <- Jan30.R2 %>% gather(CH, R2, -c(`Time(sec)`))
  Jan30.tidy<-left_join(Jan30.mO2,Jan30.R2, by = c("Time(sec)", "CH")) 
  #Jan30.tidy<-Jan30.tidy%>%select(-std.time.y)
  Jan30.tidy<-drop_na(Jan30.tidy, "mgO2")
  Jan30.tidy<-filter(Jan30.tidy,R2>.95)
#extract metabolic measures  
  Jan30.SMR<- setDT(Jan30.tidy)[, list(SMR.meanBottom10pct = mean(mgO2[mgO2<=quantile(mgO2, 0.1)])), by=CH]
  Jan30.MMR <- aggregate(mgO2 ~ CH, Jan30.tidy, max)
  Jan30.MMR<-merge(Jan30.MMR,Jan30.tidy)
  Jan30.all<-merge(Jan30.SMR,Jan30.MMR)
  Jan30.all<-rename(Jan30.all,`MMR(mgO2/kg/hr)`=mgO2)
  Jan30.all<-rename(Jan30.all,Time.MMR=`Time(sec)`)
  Jan30.all<-add_column(Jan30.all, date = "Jan30")
```

Feb6th with background in channel 3
```{r}
Feb06.kg<-get.witrox.data("06-02-2019_hour_17h.txt", lines.skip = 13,
                      delimit = "tab", choose.names = TRUE,
                      chosen.names = Proj4.background.format,
                      format = "%d-%m-%Y/%H:%M:%S")
    Feb06.temp<-select(Feb06.kg,33)
    Feb06.background<-select(Feb06.kg,c(2,4,19)) #this is specific to background in CH1 (extracting just the necessary columns for background)
    Feb06.background<-Feb06.background %>% separate(`Phase.Time(sec)`, c(NA,NA, "M"),fill = "left") #if it's just M makeing sure that it ends up on the right
    Feb06.background<-Feb06.background %>% separate(M, into = c("Phase", "M.sec"), sep = "(?<=[A-Za-z])(?=[0-9])")
    Feb06.background <- Feb06.background[grep("M", Feb06.background$Phase),] #if there is a phase with just a flush or a wait, make sure it is removed
    Feb06.background$M.sec<-as.numeric(Feb06.background$M.sec)
    Feb06.background<-filter(Feb06.background,M.sec>59) #removes the whole row
    Feb06.background <- na.omit(Feb06.background)
    ggplot(Feb06.background, aes(`Time(sec)`,`CH3.mO2[mgO2/hr]`)) + geom_point()+ geom_smooth(method = "lm") +   ggtitle("Feb06 Background")+ stat_cor(label.x = 2, label.y = .9) + stat_regline_equation(label.x = 2, label.y = 1) + ylim(-.6, 1) #plots the background oxygen consumption
    Feb06.lm<-lm(formula = `CH3.mO2[mgO2/hr]`~ `Time(sec)`, data = Feb06.background) 
    Feb06.background$adjust<-Feb06.lm$fitted.values #add a column of the background values that you are adjusted by to the origional data.frame
    Feb06.background<-select(Feb06.background,c(1,5))
    Feb06.kg <- left_join(Feb06.kg,Feb06.background, by="Time(sec)")
    Feb06.kg<-select(Feb06.kg,-c(3:4,8:11,15:18,19:25,29:32,34:35)) #removing unneeded columns (specifc to which channel has background)
#adjusting for background (specific to which channel has background)
  Feb06.kg$`CH1.mO2[mgO2/hr]`<-(Feb06.kg$`CH1.mO2[mgO2/hr]`- Feb06.kg$adjust) 
  Feb06.kg$`CH2.mO2[mgO2/hr]`<-(Feb06.kg$`CH2.mO2[mgO2/hr]`- Feb06.kg$adjust) 
  Feb06.kg$`CH4.mO2[mgO2/hr]`<-(Feb06.kg$`CH4.mO2[mgO2/hr]`- Feb06.kg$adjust)
#adjust for weight (creating new column with these values) (specific to which channel has background)
  Feb06.kg$`CH1.mO2[mgO2/kg/hr]`<-(Feb06.kg$`CH1.mO2[mgO2/hr]`/ weight_kg[c("Feb06_Chamber1"),]) 
  Feb06.kg$`CH2.mO2[mgO2/kg/hr]`<-(Feb06.kg$`CH2.mO2[mgO2/hr]`/ weight_kg[c("Feb06_Chamber2"),]) 
  Feb06.kg$`CH4.mO2[mgO2/kg/hr]`<-(Feb06.kg$`CH4.mO2[mgO2/hr]`/ weight_kg[c("Feb06_Chamber4"),])    
#reshape
  Feb06.mO2<-Feb06.kg[,c(2,15,16,17)] #not specific to different background channel, the columns extracted stay the same
  Feb06.mO2 <- rename(Feb06.mO2, CH1 = `CH1.mO2[mgO2/kg/hr]`, CH2 = `CH2.mO2[mgO2/kg/hr]`,CH4 = `CH4.mO2[mgO2/kg/hr]`) 
  Feb06.mO2 <- Feb06.mO2 %>% gather (CH, mgO2, -c(`Time(sec)`))
  Feb06.R2<-Feb06.kg[,c(2,5,8,11)] #not specific to different background channel the extracted columns stay the same
  Feb06.R2 <- rename(Feb06.R2, CH1 = CH1.R2, CH2 = CH2.R2 ,CH4 = CH4.R2)
  Feb06.R2 <- Feb06.R2 %>% gather(CH, R2, -c(`Time(sec)`))
  Feb06.tidy<-left_join(Feb06.mO2,Feb06.R2, by = c("Time(sec)", "CH")) 
  #Feb06.tidy<-Feb06.tidy%>%select(-std.time.y)
  Feb06.tidy<-drop_na(Feb06.tidy, "mgO2")
  Feb06.tidy<-filter(Feb06.tidy,R2>.95)
#extract metabolic measures  
  Feb06.SMR<- setDT(Feb06.tidy)[, list(SMR.meanBottom10pct = mean(mgO2[mgO2<=quantile(mgO2, 0.1)])), by=CH]
  Feb06.MMR <- aggregate(mgO2 ~ CH, Feb06.tidy, max)
  Feb06.MMR<-merge(Feb06.MMR,Feb06.tidy)
  Feb06.all<-merge(Feb06.SMR,Feb06.MMR)
  Feb06.all<-rename(Feb06.all,`MMR(mgO2/kg/hr)`=mgO2)
  Feb06.all<-rename(Feb06.all,Time.MMR=`Time(sec)`)
  Feb06.all<-add_column(Feb06.all, date = "Feb06")
```

Feb14th with background in channel 3
```{r}
Feb14.kg<-get.witrox.data("14-02-2019_hour_17h.txt", lines.skip = 13,
                      delimit = "tab", choose.names = TRUE,
                      chosen.names = Proj4.background.format,
                      format = "%d-%m-%Y/%H:%M:%S")
    Feb14.temp<-select(Feb14.kg,33)
    Feb14.background<-select(Feb14.kg,c(2,4,19)) #this is specific to background in CH1 (extracting just the necessary columns for background)
    Feb14.background<-Feb14.background %>% separate(`Phase.Time(sec)`, c(NA,NA, "M"),fill = "left") #if it's just M makeing sure that it ends up on the right
    Feb14.background<-Feb14.background %>% separate(M, into = c("Phase", "M.sec"), sep = "(?<=[A-Za-z])(?=[0-9])")
    Feb14.background <- Feb14.background[grep("M", Feb14.background$Phase),] #if there is a phase with just a flush or a wait, make sure it is removed
    Feb14.background$M.sec<-as.numeric(Feb14.background$M.sec)
    Feb14.background<-filter(Feb14.background,M.sec>59) #removes the whole row
    Feb14.background <- na.omit(Feb14.background)
    ggplot(Feb14.background, aes(`Time(sec)`,`CH3.mO2[mgO2/hr]`)) + geom_point()+ geom_smooth(method = "lm") +   ggtitle("Feb14 Background")+ stat_cor(label.x = 2, label.y = .9) + stat_regline_equation(label.x = 2, label.y = 1) + ylim(-.6, 1) #plots the background oxygen consumption
    Feb14.lm<-lm(formula = `CH3.mO2[mgO2/hr]`~ `Time(sec)`, data = Feb14.background) 
    Feb14.background$adjust<-Feb14.lm$fitted.values #add a column of the background values that you are adjusted by to the origional data.frame
    Feb14.background<-select(Feb14.background,c(1,5))
    Feb14.kg <- left_join(Feb14.kg,Feb14.background, by="Time(sec)")
    Feb14.kg<-select(Feb14.kg,-c(3:4,8:11,15:18,19:25,29:32,34:35)) #removing unneeded columns (specifc to which channel has background)
#adjusting for background (specific to which channel has background)
  Feb14.kg$`CH1.mO2[mgO2/hr]`<-(Feb14.kg$`CH1.mO2[mgO2/hr]`- Feb14.kg$adjust) 
  Feb14.kg$`CH2.mO2[mgO2/hr]`<-(Feb14.kg$`CH2.mO2[mgO2/hr]`- Feb14.kg$adjust) 
  Feb14.kg$`CH4.mO2[mgO2/hr]`<-(Feb14.kg$`CH4.mO2[mgO2/hr]`- Feb14.kg$adjust)
#adjust for weight (creating new column with these values) (specific to which channel has background)
  Feb14.kg$`CH1.mO2[mgO2/kg/hr]`<-(Feb14.kg$`CH1.mO2[mgO2/hr]`/ weight_kg[c("Feb14_Chamber1"),]) 
  Feb14.kg$`CH2.mO2[mgO2/kg/hr]`<-(Feb14.kg$`CH2.mO2[mgO2/hr]`/ weight_kg[c("Feb14_Chamber2"),]) 
  Feb14.kg$`CH4.mO2[mgO2/kg/hr]`<-(Feb14.kg$`CH4.mO2[mgO2/hr]`/ weight_kg[c("Feb14_Chamber4"),])    
#reshape
  Feb14.mO2<-Feb14.kg[,c(2,15,16,17)] #not specific to different background channel, the columns extracted stay the same
  Feb14.mO2 <- rename(Feb14.mO2, CH1 = `CH1.mO2[mgO2/kg/hr]`, CH2 = `CH2.mO2[mgO2/kg/hr]`,CH4 = `CH4.mO2[mgO2/kg/hr]`) 
  Feb14.mO2 <- Feb14.mO2 %>% gather (CH, mgO2, -c(`Time(sec)`))
  Feb14.R2<-Feb14.kg[,c(2,5,8,11)] #not specific to different background channel the extracted columns stay the same
  Feb14.R2 <- rename(Feb14.R2, CH1 = CH1.R2, CH2 = CH2.R2 ,CH4 = CH4.R2)
  Feb14.R2 <- Feb14.R2 %>% gather(CH, R2, -c(`Time(sec)`))
  Feb14.tidy<-left_join(Feb14.mO2,Feb14.R2, by = c("Time(sec)", "CH")) 
  #Feb14.tidy<-Feb14.tidy%>%select(-std.time.y)
  Feb14.tidy<-drop_na(Feb14.tidy, "mgO2")
  Feb14.tidy<-filter(Feb14.tidy,R2>.95)
#extract metabolic measures  
  Feb14.SMR<- setDT(Feb14.tidy)[, list(SMR.meanBottom10pct = mean(mgO2[mgO2<=quantile(mgO2, 0.1)])), by=CH]
  Feb14.MMR <- aggregate(mgO2 ~ CH, Feb14.tidy, max)
  Feb14.MMR<-merge(Feb14.MMR,Feb14.tidy)
  Feb14.all<-merge(Feb14.SMR,Feb14.MMR)
  Feb14.all<-rename(Feb14.all,`MMR(mgO2/kg/hr)`=mgO2)
  Feb14.all<-rename(Feb14.all,Time.MMR=`Time(sec)`)
  Feb14.all<-add_column(Feb14.all, date = "Feb14")

```

this has all the ave temperatures 
```{r}
library(tidyverse)
Jan27.temp<-Jan27.kg %>% select(`avg.temp[°C]`)
Jan28.temp<-Jan28.kg %>% select(`avg.temp[°C]`)
Jan29.temp<-Jan29.kg %>% select(`avg.temp[°C]`)
Jan30.temp<-Jan30.kg %>% select(`avg.temp[°C]`)
Jan31.temp<-Jan31.kg %>% select(`avg.temp[°C]`)
Feb03.temp<-Feb03.kg %>% select(`avg.temp[°C]`)
Feb04.temp<-Feb04.kg %>% select(`avg.temp[°C]`)
Feb05.temp<-Feb05.kg %>% select(`avg.temp[°C]`)
Feb06.temp<-Feb06.kg %>% select(`avg.temp[°C]`)
Feb07.temp<-Feb07.kg %>% select(`avg.temp[°C]`)
Feb11.temp<-Feb11.kg %>% select(`avg.temp[°C]`)
Feb12.temp<-Feb12.kg %>% select(`avg.temp[°C]`)
Feb13.temp<-Feb13.kg %>% select(`avg.temp[°C]`)
Feb14.temp<-Feb14.kg %>% select(`avg.temp[°C]`)
Feb15.temp<-Feb15.kg %>% select(`avg.temp[°C]`)

temp.all<-rbind(Jan27.temp,Jan28.temp, Jan29.temp, Jan30.temp, Jan31.temp, Feb03.temp, Feb04.temp, Feb05.temp, Feb06.temp, Feb07.temp, Feb11.temp, Feb12.temp, Feb13.temp, Feb14.temp, Feb15.temp)
temp.all<-temp.all%>% drop_na()

mean(temp.all$`avg.temp[°C]`)
sd(temp.all$`avg.temp[°C]`)
max(temp.all$`avg.temp[°C]`)
min(temp.all$`avg.temp[°C]`)
```

this has all the SMR and MMR 
```{r}
combo.all<-rbind(Jan27.all,Jan28.all,Jan29.all,Jan30.all,Jan31.all,Feb03.all,Feb04.all,Feb05.all,Feb06.all,Feb07.all,Feb11.all,Feb12.all,Feb13.all,Feb14.all,Feb15.all)

```

