-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBORIS aggregator.R
64 lines (28 loc) · 1.68 KB
/
BORIS aggregator.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Load required packages
library(dplyr)
library(tidyr)
# Define function to aggregate data in long format
aggregate_boris_data_long <- function(boris_data) {
# Aggregate data to calculate duration of each behavior for each focal subject and video
duration <- aggregate(duration ~ video + behavior + focal_subject, data = boris_data, sum)
# Count the number of occurrences of each behavior for each focal subject and video
count <- boris_data %>% count(video, behavior, focal_subject)
# Merge the duration and count data frames using left_join() function
merged_data <- left_join(count, duration, by = c("video", "behavior", "focal_subject"))
# Return merged data frame
return(merged_data)
}
# Define function to convert data from long to wide format
aggregate_boris_data_wide <- function(boris_data) {
# Aggregate data to calculate duration of each behavior for each focal subject and video
duration <- aggregate(duration ~ video + behavior + focal_subject, data = boris_data, sum)
# Count the number of occurrences of each behavior for each focal subject and video
count <- boris_data %>% count(video, behavior, focal_subject)
# Merge the duration and count data frames using left_join() function
merged_data <- left_join(count, duration, by = c("video", "behavior", "focal_subject"))
# Convert the data from long to wide format using pivot_wider() function
aggregated_data_wide <- merged_data %>%
pivot_wider(names_from = c(behavior, focal_subject), names_sep = ".", values_from = c(n, duration))
# Return wide-format data frame
return(aggregated_data_wide)
}