Advertisement
Guest User

Untitled

a guest
May 24th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1.  
  2.  
  3. getcleandf1 <- function(df, dateformat="%m/%d/%Y %H:%M:%S"){
  4. # 1. set up new behavior column ----
  5.  
  6. library(splitstackshape)
  7. library(data.table)
  8. library(dplyr)
  9.  
  10.  
  11. c12<-df %>% mutate(behav = ifelse(grepl("Fighting", Behavior), "Fi",
  12. ifelse(grepl("Chasing", Behavior), "Ch",
  13. ifelse(grepl("Mounting", Behavior), "Mount",
  14. ifelse(grepl("Induce-flee", Behavior), "Flee",
  15. ifelse(grepl("Subordinate", Behavior), "Sub",
  16. NA
  17. )))))
  18. )
  19.  
  20.  
  21.  
  22.  
  23. # 2. Sort by timestamp, add day and minute column----
  24. c12$date<-as.Date(c12$Timestamp, format=dateformat) #"%m/%d/%Y" #"%m/%d/%Y %H:%M:%S")
  25.  
  26. c12 <- c12 %>% arrange(date) #arrange by date
  27.  
  28. c12$day <- as.numeric(c12$date - min(c12$date) + 1)
  29. head(c12)
  30. c12$hour <- strptime(c12$Timestamp, dateformat) %>% lubridate::hour(.)-12
  31. c12$minute <- strptime(c12$Timestamp, dateformat) %>% lubridate::minute(.)
  32. c12$secs <- strptime(c12$Timestamp, dateformat) %>% lubridate::second(.)
  33.  
  34. ## Do Locations
  35.  
  36. set.seed(50)
  37. c12$Location <- ifelse(grepl(",", c12$Location), sample(strsplit(c12$Location, ", ")[[1]],1), c12$Location)
  38. c12 <- c12 %>% mutate(event = row_number())
  39.  
  40. df <- c12
  41.  
  42. dfa <- df[df$Actor!=df$Recipient,]
  43. dfb <- df[df$Actor==df$Recipient,]
  44.  
  45. dfbx <- rbind(dfb,dfb)
  46.  
  47. tmp <- matrix(unlist(strsplit(dfb$Actor, ", ")), ncol=2,byrow=T)
  48. dfbx$Actor <- c(tmp[,1],tmp[,2])
  49. dfbx$Recipient <- c(tmp[,2],tmp[,1])
  50.  
  51. df44x <- rbind(dfa,dfbx)
  52. df44x <- df44x %>% arrange(event)
  53.  
  54.  
  55.  
  56. return(df44x)
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement