我想分析一个眼睛数据文件,我需要在每个试验中计算注视数据。以下是我的数据样本,其中第3和第4列是x和y位置,第8列是固定。
75 1 76498 797 637 0 3.313 3.320 1 0
76 1 76499 793 636 0 3.320 3.321 1 0
77 1 76500 788 637 0 3.292 3.308 1 0
78 1 76501 785 636 0 3.274 3.273 1 0
79 1 76502 781 636 0 3.257 3.265 1 0
80 1 76503 775 637 0 3.257 3.250 1 0
81 1 76504 760 641 0 3.247 3.236 0 0
82 1 76505 746 644 0 3.228 3.258 0
我正在尝试创建一个搜索每个注视(由一系列注释表示)直到固定停止的功能(例如,在第82行中用0表示)然后继续进行下一次注视(第8列中的下一个1) )。对于每个试验(第二列),我希望有4个固定点,每个固定点输出均值(x)和平均值(y)以及长度(由nrow确定)。
如果有人知道使用while或for循环的简单方法,我会很高兴,因为我很难搞清楚这一点。 一切顺利,
添
答案 0 :(得分:0)
我认为这里有趣的一点是如何通过R的矢量化技术来解决它。这是一个相当简单和无聊的基于循环(非矢量化)的解决方案:
# get a list of the start and enpoints of all fixations
fixations <- vector(mode = "list")
inFixation <- FALSE
for (row in seq(nrow(df)) {
# if we have no active fixation and we see a 1, add new fixation
if (!isTRUE(inFixation) && df[row, 8] == 1) {
fixations[[length(fixations) + 1]] <- c("start" = row, "end" = NA)
inFixation <- TRUE
}
# if we have an active fixation and we see a 0, update end of current fixation
if (isTRUE(inFixation) && (df[row, 8] == 0 || row == nrow(df))) {
fixations[[length(fixations)]]["end"] <- row - 1
inFixation <- FALSE
}
}
# then you can get the mean x, y coordinates via
lapply(fixations, function(fixation) {
c(
"mean.x" = mean(df[seq(from = fixation["start"], to = fixation["end"], by = 1), 3],
"mean.y" = mean(df[seq(from = fixation["start"], to = fixation["end"], by = 1), 4]
)
})