diff --git a/R/behavioral_data_preprocessing.R b/R/behavioral_data_preprocessing.R index b910395..ba10814 100644 --- a/R/behavioral_data_preprocessing.R +++ b/R/behavioral_data_preprocessing.R @@ -6,17 +6,36 @@ #' @note Use the .interval variable if 8 seconds is not the desired interval #' @export get_hit_times <- function(signal_times, response_times, .interval = 8.0) { + + # Create vector to hold hit indices + hit_indices <- c() + signal_times %>% map_dbl(function(signal_time) { - hit_index <- first(which( + # Find indices for potential hits within the signal interval + potential_hit_indices <- which( response_times %>% map_lgl(~ between(.x, signal_time, signal_time + .interval)), arr.ind = TRUE - )) + ) + + # If there are zero potential hits for this signal time, return NA + if (!length(potential_hit_indices)) return(NA) + # Remove existing hit indices from potential hit indices for this signal + potential_hit_indices <- setdiff(potential_hit_indices, hit_indices) + + # Extract the first hit index (closest to signal time) + hit_index <- first(potential_hit_indices) + + # Extract hit time from response times using hit index hit_time <- response_times[hit_index] + # Update vector of hit indices + hit_indices <- c(hit_indices, hit_index) + + # Return the hit time return(hit_time) }) diff --git a/notebooks/behavioral_data_preprocessing.Rmd b/notebooks/behavioral_data_preprocessing.Rmd index b01bc5f..1378adf 100644 --- a/notebooks/behavioral_data_preprocessing.Rmd +++ b/notebooks/behavioral_data_preprocessing.Rmd @@ -72,11 +72,11 @@ combined_df %>% summarise(mean(n), sd(n), min(n), max(n)) ``` -## Function definitions +## Combined hits dataframe for all participants The function to get each participant's hit times vector is `itrackvalr::get_hit_times`, and the function to get a dataframe of all participants' hits is `itrackvalr::get_all_hits_with_reaction_times`, including each hit time and reaction time between that hit time and the nearest signal prompting a response within a fixed interval. -## Get the combined hits using the function +Get the combined hits using the function: ```{r} combined_hits_df <- get_all_hits_with_reaction_times(participants, combined_df) diff --git a/notebooks/behavioral_data_preprocessing.html b/notebooks/behavioral_data_preprocessing.html index dbac0ed..1fc3916 100644 --- a/notebooks/behavioral_data_preprocessing.html +++ b/notebooks/behavioral_data_preprocessing.html @@ -605,8 +605,7 @@

Behavioral Data Preprocessing