Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- getcleandf1 <- function(df, dateformat="%m/%d/%Y %H:%M:%S"){
- # 1. set up new behavior column ----
- library(splitstackshape)
- library(data.table)
- library(dplyr)
- c12<-df %>% mutate(behav = ifelse(grepl("Fighting", Behavior), "Fi",
- ifelse(grepl("Chasing", Behavior), "Ch",
- ifelse(grepl("Mounting", Behavior), "Mount",
- ifelse(grepl("Induce-flee", Behavior), "Flee",
- ifelse(grepl("Subordinate", Behavior), "Sub",
- NA
- )))))
- )
- # 2. Sort by timestamp, add day and minute column----
- c12$date<-as.Date(c12$Timestamp, format=dateformat) #"%m/%d/%Y" #"%m/%d/%Y %H:%M:%S")
- c12 <- c12 %>% arrange(date) #arrange by date
- c12$day <- as.numeric(c12$date - min(c12$date) + 1)
- head(c12)
- c12$hour <- strptime(c12$Timestamp, dateformat) %>% lubridate::hour(.)-12
- c12$minute <- strptime(c12$Timestamp, dateformat) %>% lubridate::minute(.)
- c12$secs <- strptime(c12$Timestamp, dateformat) %>% lubridate::second(.)
- ## Do Locations
- set.seed(50)
- c12$Location <- ifelse(grepl(",", c12$Location), sample(strsplit(c12$Location, ", ")[[1]],1), c12$Location)
- c12 <- c12 %>% mutate(event = row_number())
- df <- c12
- dfa <- df[df$Actor!=df$Recipient,]
- dfb <- df[df$Actor==df$Recipient,]
- dfbx <- rbind(dfb,dfb)
- tmp <- matrix(unlist(strsplit(dfb$Actor, ", ")), ncol=2,byrow=T)
- dfbx$Actor <- c(tmp[,1],tmp[,2])
- dfbx$Recipient <- c(tmp[,2],tmp[,1])
- df44x <- rbind(dfa,dfbx)
- df44x <- df44x %>% arrange(event)
- return(df44x)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement