简体   繁体   English

使用 if else 语句创建可根据呈现的刺激进行调整的灵活公式

[英]Using if else statements to create a flexible formula that can adjust based on stimuli being presented

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.具体而言,每个刺激之间的 x 和 y 坐标变化很小。 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.如果我能读懂这些就太好了。将其与 Final_Label 数据中的 Stimuli 匹配,并能够计算和汇总所有试验中的这些指标。 So I need something within the code that essentially says - if it is this stimuli (eg, A1 use these coordinates etc).所以我需要在代码中说明一些内容 - 如果是这种刺激(例如,A1 使用这些坐标等)。

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 数据框的示例

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))

Here's code to remove the ".jpg" and join based on the Stimuli values.这是删除“.jpg”并根据刺激值加入的代码。

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.我使用full_join将保留两个数据框中的所有行。 You can use inner , left , or right joins instead if you want different behavior.如果您想要不同的行为,您可以改用innerleft连接或right连接。 (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. (内部将仅保留匹配的行,左侧和右侧将保留第一个或第二个数据帧(分别)中的所有行以及来自另一个数据帧的任何匹配的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM