R/CButtonDate_function.R

Defines functions CButtonDate

Documented in CButtonDate

#' ReVuePro: CButtonDate
#'
#' A simple function to take a .csv output from WeeButton (TM) software, clean it, and re-organize it into something more useful. This function requires that .csv files be named by date, in YYYYMMDD format (ie 20180523.csv) 
#' @param Year A four digit number, designating the year that the iButton data was collected in.
#' @param Month A two digit number, designating the month that the iButton data was collected in.
#' @param Day A two digit number, designating the day that the iButton data was collected in.
#' @keywords WeeButton, iButton
#' @import dplyr rlang
#' @export
#' @examples
#' setwd("C:/Users/Tabby/Desktop/Cat")
#' TheTemperatureofKittens<-as.data.frame(CButtonDate(2018,05,23))
#' print(TheTemperatureofKittens)

CButtonDate<-function(Year, Month, Day){
             require('rlang')
             require('dplyr')

             if(nchar(Month) == 1){
                Month<-as.character(paste("0",Month,sep=""))
                }
             if(nchar(Day) == 1){
                Day<-as.character(paste("0",Day,sep=""))
                }

             Date<-paste(Year,Month,Day,sep="")
             iButton<-read.csv(paste(Date, "csv", sep="."))

             iButton<-iButton[-c(1:33),c(1,3)]
             colnames(iButton)=c("Time","Temperature")

{
   AM.PM<-c()
   if(length(i<- grep("AM", iButton[,1])))
   cat("'AM' appears at least once in\n\t", iButton[,1], "\n")
   AM.PM[i]<-"AM"
   
   if(length(j<- grep("PM", iButton[,1])))
   cat("'PM' appears at least once in\n\t", iButton[,1], "\n")
   AM.PM[j]<-"PM"
   
   iButton$AMPM<-AM.PM
   iButton[,1]<-c(gsub(" AM*", "",iButton[,1]))
   iButton[,1]<-c(gsub(" PM*", "",iButton[,1]))

   if(length(grep("/", iButton[,1]))>= 1){
     Temp.Type="Ugly"
     Month<-gsub("*0","",Month)
        if(Day <=9){
        Day<-gsub("*0","",Day)}
     iButton[,1]<-gsub(paste(paste(Month,Day,Year, sep="/"), " ", sep=""), "",iButton[,1]) 
     } else {
     Temp.Type="Proper"
     iButton[,1]<-gsub(paste(paste(Year,Month,Day, sep="-"), " ", sep=""), "",iButton[,1]) 
     }
 
    iButton$Date<-Date
   
    hours<-c()
    for(i in 1:nrow(iButton)){
    hours[i]<-c(sapply(strsplit(iButton[i,1], ":"), function(n) as.numeric(n)))}
    iButton$temp<-hours

   rearr<-vector('list', nrow(iButton))
   for(i in length(rearr))
   rearr[[i]]<-c()
   global.sec<-c()
     for(i in 1:nrow(iButton)){
     rearr[[i]]<-c(sapply(strsplit(iButton[i,1], ":"), function(n) as.numeric(n)))}

     for(i in 1:length(rearr)){
     if(iButton[i,3]=="AM")
     global.sec[i]<-rearr[[i]][1]*3600+rearr[[i]][2]*60+rearr[[i]][3]
     else(global.sec[i]<-(rearr[[i]][1]+12)*3600+rearr[[i]][2]*60+rearr[[i]][3])
     }

     rm<-c(which(iButton$temp==12))
     global.sec[rm]<-global.sec[rm]-(12*3600)
     
     iButton<-iButton[,-5]
     iButton$TIS<-global.sec
     
    iButton = iButton %>% select(Date, Time, AMPM, TIS, Temperature)
    }
    name<-paste(paste(Date, "rMod", sep=""),"csv",sep=".")
    write.csv(iButton, name, row.names=FALSE)
    }
joshuakrobertson/R-Package_ReVuePro documentation built on June 2, 2020, 8:23 p.m.