这是我第一次在这个论坛上提问。我希望你可以帮助我。 我有一个包含眼动追踪数据的数据文件。在数据文件中,每一行都是 8.3 毫秒的样本。 变量 ID 计算整个会话中记录的样本数量。变量 TrialID 列出了试验编号。我想要的是
这是相关列的示例
Subject ID TrialId Status
253 1 1 Fixation1
253 2 1 Fixation1
253 3 1 Fixation1
253 4 1 Fixation1
253 34 1 Preview1
253 35 1 Preview1
253 36 1 Preview1
253 66 1 Show1
253 67 1 Show1
253 68 1 Show1
253 69 1 Show1
253 70 1 Show1
253 134 2 Fixation1
253 135 2 Fixation1
253 150 2 Preview1
253 151 2 Preview1
253 152 2 Preview1
253 234 2 Show1
253 235 2 Show1
253 236 2 Show1
253 237 2 Show1
300 1 1 Fixation1
300 2 1 Fixation1
300 3 1 Preview1
300 44 1 Preview1
300 45 1 Preview1
300 46 1 Show1
300 47 1 Show1
300 48 1 Show1
300 49 1 Show1
我想要的输出如下所示:
Subject ID TrialId Status Binnr Time
253 1 1 Fixation1 1 0
253 2 1 Fixation1 2 8.3
253 3 1 Fixation1 3 16.6
253 4 1 Fixation1 4 24.9
253 34 1 Preview1 1 0
253 35 1 Preview1 2 8.3
253 36 1 Preview1 3 16.6
253 66 1 Show1 1 0
253 67 1 Show1 2 8.3
253 68 1 Show1 3 16.6
253 69 1 Show1 4 24.9
253 70 1 Show1 5 33.2
253 134 2 Fixation1 1 0
253 135 2 Fixation1 2 8.3
253 150 2 Preview1 1 0
253 151 2 Preview1 2 8.3
253 152 2 Preview1 3 16.6
253 234 2 Show1 1 0
253 235 2 Show1 2 8.3
253 236 2 Show1 3 16.6
253 237 2 Show1 4 24.9
300 1 1 Fixation1 1 0
300 2 1 Fixation1 2 8.3
300 3 1 Preview1 1 0
300 44 1 Preview1 2 8.3
300 45 1 Preview1 3 16.6
300 46 1 Show1 1 0
300 47 1 Show1 2 8.3
300 48 1 Show1 3 16.6
300 49 1 Show1 4 24.9
我尝试创建一个 for 循环,在行上循环,每次遇到主题编号/TrialID 和状态级别时,它都会向空列 binnr 添加一个数字。 我使用的代码的开头
for(i in 1:length(dat)) {
if (dat$Status == "Show1"& dat$TrialID == "1" & Subject == "223")
en 然后附加一个值 已经没有意义了,因为列出组合的效率太低了。 我可以展示更多代码,但如果您能给我一个开始的帮助,我将不胜感激
我尝试创建一个 for 循环,在行上循环,每次遇到主题编号/TrialID 和状态级别时,它都会向空列 binnr 添加一个数字。 我使用的代码的开头
for(i in 1:length(dat)) {
if (dat$Status == "Show1"& dat$TrialID == "1" & Subject == "223")
en 然后附加一个值 已经没有意义了,因为列出组合的效率太低了。 我可以展示更多代码,但是有很多初学者的错误和缺陷。如果您能帮助我开始循环不同的列,我将不胜感激
您应该能够使用
dplyr::mutate()
来完成此操作 - Binnr
只是每组中的行数,因此请使用 dplyr::row_number()
。 Time
使用基 R seq()
创建一个从 0 到每组中最大行数的序列(使用 dplyr::n()
),以段 8.3 计数。
df %>%
mutate(Binnr = row_number(),
Time = seq(0, ((n()-1)*8.3), length.out = n()),
.by = c(Status, TrialId, Subject))
输出
# Subject ID TrialId Status Binnr Time
# 1 253 1 1 Fixation1 1 0.0
# 2 253 2 1 Fixation1 2 8.3
# 3 253 3 1 Fixation1 3 16.6
# 4 253 4 1 Fixation1 4 24.9
# 5 253 34 1 Preview1 1 0.0
# 6 253 35 1 Preview1 2 8.3
# 7 253 36 1 Preview1 3 16.6
# 8 253 66 1 Show1 1 0.0
# 9 253 67 1 Show1 2 8.3
# 10 253 68 1 Show1 3 16.6
# 11 253 69 1 Show1 4 24.9
# 12 253 70 1 Show1 5 33.2
# 13 253 134 2 Fixation1 1 0.0
# 14 253 135 2 Fixation1 2 8.3
# 15 253 150 2 Preview1 1 0.0
# 16 253 151 2 Preview1 2 8.3
# 17 253 152 2 Preview1 3 16.6
# 18 253 234 2 Show1 1 0.0
# 19 253 235 2 Show1 2 8.3
# 20 253 236 2 Show1 3 16.6
# 21 253 237 2 Show1 4 24.9
# 22 300 1 1 Fixation1 1 0.0
# 23 300 2 1 Fixation1 2 8.3
# 24 300 3 1 Preview1 1 0.0
# 25 300 44 1 Preview1 2 8.3
# 26 300 45 1 Preview1 3 16.6
# 27 300 46 1 Show1 1 0.0
# 28 300 47 1 Show1 2 8.3
# 29 300 48 1 Show1 3 16.6
# 30 300 49 1 Show1 4 24.9
我会提供这个选项:
library(dplyr)
df %>%
mutate(Binnr = row_number(), .by =c("Status","TrialId", "Subject"),
Time = 8.3 * (Binnr-1))
或使用单个命令:
mutate(df, Binnr = row_number(), .by =c("Status","TrialId", "Subject"),
Time = 8.3 * (Binnr-1))
输出:
Subject ID TrialId Status Binnr Time
1 253 1 1 Fixation1 1 0.0
2 253 2 1 Fixation1 2 8.3
3 253 3 1 Fixation1 3 16.6
4 253 4 1 Fixation1 4 24.9
5 253 34 1 Preview1 1 0.0
6 253 35 1 Preview1 2 8.3
7 253 36 1 Preview1 3 16.6
8 253 66 1 Show1 1 0.0
9 253 67 1 Show1 2 8.3
10 253 68 1 Show1 3 16.6
11 253 69 1 Show1 4 24.9
12 253 70 1 Show1 5 33.2
13 253 134 2 Fixation1 1 0.0
14 253 135 2 Fixation1 2 8.3
15 253 150 2 Preview1 1 0.0
16 253 151 2 Preview1 2 8.3
17 253 152 2 Preview1 3 16.6
18 253 234 2 Show1 1 0.0
19 253 235 2 Show1 2 8.3
20 253 236 2 Show1 3 16.6
21 253 237 2 Show1 4 24.9
22 300 1 1 Fixation1 1 0.0
23 300 2 1 Fixation1 2 8.3
24 300 3 1 Preview1 1 0.0
25 300 44 1 Preview1 2 8.3
26 300 45 1 Preview1 3 16.6
27 300 46 1 Show1 1 0.0
28 300 47 1 Show1 2 8.3
29 300 48 1 Show1 3 16.6
30 300 49 1 Show1 4 24.9
使用数据:
df <- structure(list(Subject = c(253L, 253L, 253L, 253L, 253L, 253L,
253L, 253L, 253L, 253L, 253L, 253L, 253L, 253L, 253L, 253L, 253L,
253L, 253L, 253L, 253L, 300L, 300L, 300L, 300L, 300L, 300L, 300L,
300L, 300L), ID = c(1, 2, 3, 4, 34, 35, 36, 66, 67, 68, 69, 70,
134, 135, 150, 151, 152, 234, 235, 236, 237, 1, 2, 3, 44, 45,
46, 47, 48, 49), TrialId = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L), Status = c("Fixation1", "Fixation1",
"Fixation1", "Fixation1", "Preview1", "Preview1", "Preview1",
"Show1", "Show1", "Show1", "Show1", "Show1", "Fixation1", "Fixation1",
"Preview1", "Preview1", "Preview1", "Show1", "Show1", "Show1",
"Show1", "Fixation1", "Fixation1", "Preview1", "Preview1", "Preview1",
"Show1", "Show1", "Show1", "Show1")), class = "data.frame", row.names = c(NA,
-30L))