0

I hope all is well. I recently developed a code (with the help of the wonderful stack overflow community) to calculate time to first fixation, first fixation duration, and total visit duration to areas of interest. I now want to update this so it can adjust based on the stimuli being presented. Specifically the x and y coordinates vary ever so slightly between each stimuli. I have an example spreadsheet of the stimuli coordinates. It would be great if I could read those in. Match it to the Stimuli in the Final_Label data and be able to calculate and summarize these metrics across all trials. So I need something within the code that essentially says - if it is this stimuli (e.g., A1 use these coordinates etc).

Thank you for your help and please let me know if I can provide any further information at this time.

Take care and stay well,

Caroline

Face_AOI <- Final_Labels %>%
    mutate(AOI_face = (mean_x >= .100 & mean_x <= .500 & mean_y >= .100 & mean_y <= .800), #These numbers are FAKE ###) %>%
    filter(AOI_face) %>%
    group_by(SubjectID, Trial) %>%
    summarize(Face_totalfixationduration = sum(Duration), Face_firstfixationduration = first(Duration), Face_timetofirstfixation = first(Start))
  
  
  Mouth_AOI <- Final_Labels %>%
    mutate(AOI_mouth = (mean_x >= .200 & mean_x <= .300 & mean_y >= .500 & mean_y <= .600)) %>%
    filter(AOI_mouth) %>%
    group_by(SubjectID, Trial) %>%
    summarize(Mouth_totalfixationduration = sum(Duration), Mouth_firstfixationduration = first(Duration), Mouth_timetofirstfixation = first(Start))
  
  Mouth_AOI$SubjectID <- NULL
  
  Eyes_AOI <- Final_Labels %>%
    mutate(
           AOI_eyes = (mean_x >= .200 & mean_x <= .300 & mean_y >= .500 & mean_y <= .600)) %>%
    filter(AOI_eyes) %>%
    group_by(SubjectID, Trial) %>%
    summarize(Eyes_totalfixationduration = sum(Duration), Eyes_firstfixationduration = first(Duration), Eyes_timetofirstfixation = first(Start))
  

Example of a list of stimuli with different coordinates to integrate into the above code.


Df2 <- data.frame(Stimuli = c("A1", "A1", "A1", "A2", "A2", "A2"),
AOI = C("Face", "Eyes", "Mouth", "Face", "Eyes", "Mouth"),
X1 = c(0, 300, 301, 0, 305, 306),
X2 = c(1022, 600, 600, 0, 604, 604),
Y1 = c(0, 30, 31, 0, 30, 38),
Y2 = c(0, 300, 301, 6, 305, 306))

Here is an example of the Final_Labels dataframe

Final_Labels <- data.frame(Stimuli = c("A1.jpg", "A2.jpg", "A3.jpg", "A4.jpg", "A5.jpg", "H1.jpg"), ##note we will need .jpg to be removed to match the files
Duration = c(300, NA, 300, 60, NA, NA),
Start = c(100, NA, 1, 100, NA, NA),
End = c(160, NA, 301, 160, NA, NA),
mean_x = c(.3, NA, .50, .40, NA, NA),
mean_y = c(.5, NA, .4, .5, NA, NA))
Caroline
  • 37
  • 6
  • 1
    Thank you for pointing this out. I have edited the post and this should be fixed now. – Caroline Oct 30 '20 at 19:05
  • Okay - I came back to look at this and I'm a bit confused as to your goal. I don't have an example of what `Final_Label` looks like, so I don't really understand where you're starting. Matching your `Df2` to `Final_Label` probably means a "join" of some type - you can see the FAQ [How to join (merge) data in R?](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right). – Gregor Thomas Nov 02 '20 at 14:13
  • If you provide a sample of `Final_Label` as well as a little bit more precise description of how you want them combined, I can take a stab at it. (Desired output coresponding to your sample input would be helpful) – Gregor Thomas Nov 02 '20 at 14:13
  • One last note - the `X1, X2, Y1, Y2` you provide are all `character` class, not numeric (because of the quotes around them). Is this deliberate? If you want to use them as numbers, probably the first step would be to convert them all to numeric. – Gregor Thomas Nov 02 '20 at 14:14
  • Hi Gregor thank you for your continued assistance. It is truly appreciated. I have now added an example of the Final_Labels data frame. In regards to your question X1, X2, Y1, Y2 are all already numeric - I have removed the quotes to better reflect this. – Caroline Nov 03 '20 at 17:28

1 Answers1

0

Here's code to remove the ".jpg" and join based on the Stimuli values.

library(dplyr)
library(stringr)
Final_Labels %>%
  mutate(Stimuli = str_remove(Stimuli, fixed(".jpg"))) %>%
  full_join(Df2)
#    Stimuli Duration Start End mean_x mean_y   AOI  X1   X2 Y1  Y2
# 1       A1      300   100 160    0.3    0.5  Face   0 1022  0   0
# 2       A1      300   100 160    0.3    0.5  Eyes 300  600 30 300
# 3       A1      300   100 160    0.3    0.5 Mouth 301  600 31 301
# 4       A2       NA    NA  NA     NA     NA  Face   0    0  0   6
# 5       A2       NA    NA  NA     NA     NA  Eyes 305  604 30 305
# 6       A2       NA    NA  NA     NA     NA Mouth 306  604 38 306
# 7       A3      300     1 301    0.5    0.4  <NA>  NA   NA NA  NA
# 8       A4       60   100 160    0.4    0.5  <NA>  NA   NA NA  NA
# 9       A5       NA    NA  NA     NA     NA  <NA>  NA   NA NA  NA
# 10      H1       NA    NA  NA     NA     NA  <NA>  NA   NA NA  NA

I used full_join which will keep all rows from both data frames. You can use inner, left, or right joins instead if you want different behavior. (Inner will keep only rows that match, left and right will keep all rows from the 1st or 2nd data frame (respectively) and any that match from the other.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294