############################## # # Script for preparing Observation data from BORIS (Friard & Gamba, 2016) behavioural data # Need to have duration of behaviour per individual per observation # 12/12/19 # # ############################### DIR <- # filepath to project directory DATADIR <- # filepath to data directory if (!require("tidyr")) install.packages("tidyr") library(tidyr) if (!require("data.table")) install.packages("data.table") library(data.table) if (!require("chron")) install.packages("chron") library(chron) # Define function called 'DurationPerSession' that calculates the duration of each behaviour per observation period DurationPerSession <- function(COLONY){ # define input from files named COLONY_1.tsv (behavioural data) and COLONY_Info.csv (individual data), where COLONY = colony name ColonyBehaviour <- read.csv(file.path(DATADIR,paste(COLONY,"_1.tsv", sep="")), sep="\t") #dataframe containing raw behaviour data Beh <- ColonyBehaviour ColonyInformation <- read.table(file.path(DATADIR,paste(COLONY,"_Info.csv", sep="")), sep=",", header=T) #dataframe containing individual information Ind <- ColonyInformation names(Ind)[1] <- 'ObsID' # rename subject name variable # remove Media file and FPS, Start and End times, split date into time and date, sort out modifiers Beh$Media.file <- NULL Beh$FPS <- NULL Beh$Start..s. <- NULL Beh$Stop..s. <- NULL Beh$Comment.start <- NULL Beh$Comment.stop <- NULL Beh$Behavior.type <- NULL Beh$Behavioral.category <- NULL # to split the date time and format as date and time Beh <- separate(data=Beh, col=Observation.date, into= c("Date", "Time"), sep="\\ ") # converts the original date from a character to a date. New format "%Y/%m/%d" # exported files from BORIS can have different date formats: # "%d/%m/%Y" or "%Y-%m-%d". This has to be changed manually, below, but should end up in format "%Y/%m/%d" Beh$Date <- as.Date(Beh$Date, "%d/%m/%Y") if (is.na(Beh$Date)==T) { Beh <- ColonyBehaviour Beh <- separate(data=Beh, col=Observation.date, into= c("Date", "Time"), sep="\\ ") Beh$Date <- as.Date(Beh$Date, "%Y-%m-%d") Beh$Media.file <- NULL Beh$FPS <- NULL Beh$Start..s. <- NULL Beh$Stop..s. <- NULL Beh$Comment.start <- NULL Beh$Comment.stop <- NULL Beh$Behavior.type <- NULL Beh$Behavioral.category <- NULL } # Separate and data from modifiers column in BORIS output into appropriate variables Beh <- separate(data=Beh, col=Modifiers, into= c("Partner", "Role", "Activity_Before", "Activity_After"), sep="\\|") # separate modifiers column row.ind<- which( Beh$Behavior=="Aggression") # outputs numbers of the rows in which Behaviour == Aggression Beh2 <- Beh # make a copy Beh2$Partner[row.ind] <- Beh$Role[row.ind] # switch Partner and Role data as they separate into the wrong columns Beh2$Role[row.ind] <- Beh$Partner[row.ind] Beh <- Beh2 row.ind2<- which( Beh$Behavior=="Tail-tugging") # outputs number of the rows in which Behaviour == Tail-tugging Beh2 <- Beh # make a copy Beh2$Partner[row.ind2] <- Beh$Role[row.ind2] # switch Partner and Role data as they separate into the wrong columns Beh2$Role[row.ind2] <- Beh$Partner[row.ind2] Beh <- Beh2 colnames(Beh) <- c("ObsSession","ObsDate","ObsTime", "ObsDuration_seconds","Subject","Behaviour", "Partner","Role", "Activity_Before", "Activity_After", "BehaviourDuration_seconds") # To sum durations per individual per observation period Durations <- subset(Beh, (!is.na(Beh$BehaviourDuration_seconds))) # to subset only behaviours that have a duration DurationsSums <- aggregate(BehaviourDuration_seconds ~ Subject + ObsSession + ObsTime + ObsDate, data = Durations, FUN = 'sum') # to sum per individual per observation colnames(DurationsSums) <- c("ObsID", "ObsSession", "ObsTime", "ObsDate", "BehaviourDuration_seconds") # to merge DurationSums and Ind keeping only ObsID variable setDT(DurationsSums)[setDT(Ind), TagNo := i.TagNo, on=c("ObsID")] # Add colony to the data frame DurationsSums$Colony <- COLONY # write results to Master data count file write.table(DurationsSums, file.path(paste(DATADIR,"OUTPUT_NAME.txt", sep="")), sep="\t", append = T, row.names = FALSE, col.names=!file.exists(file.path(paste(DATADIR,"OUTPUT_NAME.txt", sep=""))), quote=F) # colnames true for first colony } # to make a list of colonies to loop the function through ColonyList<- gsub("_.*","", (tools::file_path_sans_ext(list.files(path=DATADIR, pattern="_1.tsv", full.names = F)))) # Run function over list of colonies for (C in ColonyList) { print(C) DurationPerSession(C) } # check warning errors generated by function warnings()