R 眼动输出中的计数注视

R 眼动输出中的计数注视,r,R,我想分析一个眼睛数据文件,其中我需要计算每个试验中的注视次数。这是我的数据示例,其中第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 7650

我想分析一个眼睛数据文件,其中我需要计算每个试验中的注视次数。这是我的数据示例,其中第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循环的简单方法,我会很高兴,因为我很难弄明白这一点。 祝你一切顺利


Tim

我认为这里有趣的一点是如何通过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]
  )
})
#获取所有固定点的起点和终点列表

修正我很抱歉,如果我的问题不完整或不符合stackoverflow惯例,这是我在这里的第一个问题。您能说明输出的情况吗?请提供有关数据结构的其他信息。第一列实际上是列名还是行名?固定是在最后一次
1
还是在第一次
0
时停止?