gpt4 book ai didi

R:根据时间列每次试验的排名变量

转载 作者:行者123 更新时间:2023-12-04 13:38:05 26 4
gpt4 key购买 nike

我的数据如下所示:

Subject  Trial  Task  Time  Fixation
..
1 1 2 1 0.335
1 1 2 456 NA
1 1 2 765 0.165
1 1 2 967 0.445
..
2 3 1 1 0.665
2 3 1 300 0.556
2 3 1 570 NA
2 3 1 900 NA
..
15 5 3 1 0.766
15 5 3 567 0.254
15 5 3 765 0.167
15 5 3 1465 NA
..

我想创建一个列 FixationID,我想根据时间列(1,2,3,4..)对每个试验 的每个 Fixation 进行排名。时间列以毫秒为单位显示每个试验的时间进程,每个试验从 1 开始。试验有不同的长度。

我希望我的数据看起来像这样:
Subject  Trial  Task  Time  Fixation FixationID
..
1 1 2 1 0.335 1
1 1 2 456 NA NA
1 1 2 765 0.165 2
1 1 2 967 0.445 3
..
2 3 1 1 0.665 1
2 3 1 300 0.556 2
2 3 1 570 NA NA
2 3 1 900 NA NA
..
15 5 3 1 0.766 1
15 5 3 567 0.254 2
15 5 3 765 0.167 3
15 5 3 1465 NA NA
..

我试过
library(data.table)
setDT(mydata)[!is.na(Fixation), FixID :=
seq_len(.N)[order(Time)], by = Trial]

但我得到的是 1,16,31,45,57.. 对于我的 Subject 1 Trial 1. 我想要 1,2,3,4,5..

谁能帮我这个?

摘自我的数据:
structure(list(Subject = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), Trial = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L), Task = c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L), Time = c(1L, 385L, 571L, 638L, 951L,
1020L, 1349L, 1401L, 1661L, 1706L, 2042L, 2067L, 2322L, 2375L,
2540L, 2660L, 2686L, 3108L, 3172L, 3423L, 3462L, 3845L, 3870L,
3969L, 4099L, 4132L, 1L, 471L, 513L, 697L), Fixation = c(0.383,
0.185, NA, 0.312, NA, 0.328, NA, 0.259, NA, 0.335, NA, 0.254,
NA, 0.164, 0.119, NA, 0.421, NA, 0.25, NA, 0.382, NA, 0.0979999999999999,
0.129, NA, 0.335, 0.469, NA, 0.183, NA)), .Names = c("Subject",
"Trial", "Task", "Time", "Fixation"), row.names = c(NA, 30L), class = "data.frame")

最佳答案

那这个呢:

library(data.table)
setDT(mydata)
mydata[!is.na(Fixation), FixID := frank(Time), by = Trial]

head(mydata, 10)
Subject Trial Task Time Fixation FixID
1: 1 1 2 1 0.383 1
2: 1 1 2 385 0.185 2
3: 1 1 2 571 NA NA
4: 1 1 2 638 0.312 3
5: 1 1 2 951 NA NA
6: 1 1 2 1020 0.328 4
7: 1 1 2 1349 NA NA
8: 1 1 2 1401 0.259 5
9: 1 1 2 1661 NA NA
10: 1 1 2 1706 0.335 6

tail(mydata, 10)
Subject Trial Task Time Fixation FixID
1: 1 1 2 3462 0.382 12
2: 1 1 2 3845 NA NA
3: 1 1 2 3870 0.098 13
4: 1 1 2 3969 0.129 14
5: 1 1 2 4099 NA NA
6: 1 1 2 4132 0.335 15
7: 1 2 2 1 0.469 1
8: 1 2 2 471 NA NA
9: 1 2 2 513 0.183 2
10: 1 2 2 697 NA NA

关于R:根据时间列每次试验的排名变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60656752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com