You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于df2阈值独立过滤df1中各被试的RT数据?

问题描述

需要基于另一个数据框中的被试专属阈值过滤反应时(RT)数据,具体要求:

  • 为df1中的每个被试独立过滤RT值,仅保留满足 RT > df2$RTLowerLimit 且 RT < df2$RTUpperLimit 的数据
  • 将过滤结果存入df1的新变量RT_filtered

df1前15行数据

ExperimentName        Subject Block practisexperimental Trial  Congruency     Acc RT
1   Bayesmicro-53-SINGLE       1     2        experimental     1   Congruent        1 549
2   Bayesmicro-53-SINGLE       1     2        experimental     2 Incongruent        1 510
3   Bayesmicro-53-SINGLE       1     2        experimental     3   Congruent        1 476
4   Bayesmicro-53-SINGLE       1     2        experimental     4   Congruent        1 568
5   Bayesmicro-53-SINGLE       1     2        experimental     5   Congruent        1 401
6   Bayesmicro-53-SINGLE       1     2        experimental     6 Incongruent        1 458
7   Bayesmicro-53-SINGLE       1     2        experimental     7 Incongruent        1 494
8   Bayesmicro-53-SINGLE       1     2        experimental     8 Incongruent        1 876
9   Bayesmicro-53-SINGLE       1     2        experimental     9 Incongruent        1 567
10  Bayesmicro-53-SINGLE       1     2        experimental    11   Congruent        1 444
11  Bayesmicro-53-SINGLE       1     2        experimental    13 Incongruent        1 507
12  Bayesmicro-53-SINGLE       1     2        experimental    14 Incongruent        1 658
13  Bayesmicro-53-SINGLE       1     2        experimental    15 Incongruent        1 613
14  Bayesmicro-53-SINGLE       1     2        experimental    16   Congruent        1 529
15  Bayesmicro-53-SINGLE       1     2        experimental    18 Incongruent        1 513

df2完整数据

Subject Mean_RT SD_RT   RTUpperLimit  RTLowerLimit
 1 1          485. 102.          688.        281. 
 2 10         596. 143.          881.        311. 
 3 11         608. 149.          907.        309. 
 4 12         546.  89.9         726.        366. 
 5 13         465.  81.3         627.        302. 
 6 14         559. 232.         1024.         93.8
 7 15         464.  66.4         597.        332. 
 8 16         803. 174.         1152.        455. 
 9 17         598. 124.          846.        350. 
10 18         485.  83.1         651.        319. 
11 19         483. 204.          892.         74.3
12 2          548. 144.          835.        260. 
13 20         547. 111.          769.        326. 
14 3          496. 100.          696.        295. 
15 4          576. 165.          906.        245. 
16 5          546. 122.          789.        303. 
17 6          543. 169.          882.        204. 
18 7          514.  93.1         700.        328. 
19 8          578. 118.          814.        341. 
20 9          556.  99.4         755.        358. 
解决方案

使用dplyr工具包,先通过left_join按Subject匹配两个数据框的阈值,再生成过滤后的RT变量:

library(dplyr)

# 匹配阈值并生成过滤后的RT变量
df1 <- df1 %>%
  left_join(df2, by = "Subject") %>%
  mutate(RT_filtered = ifelse(RT > RTLowerLimit & RT < RTUpperLimit, RT, NA)) %>%
  # 可选:移除连接进来的冗余阈值列,保留原df1结构
  select(-Mean_RT, -SD_RT, -RTUpperLimit, -RTLowerLimit)

关键说明

  1. 按被试匹配阈值:left_join确保df1的所有行都被保留,同时精准匹配对应被试的上下限阈值
  2. 过滤逻辑实现:ifelse判断每个RT是否在阈值区间内,符合条件保留原值,不符合则标记为NA(可根据需求替换为其他标记)
  3. 结构保留:最后一步select用于移除临时引入的阈值列,若需要查看阈值可跳过此步

以被试1为例,其RT上限为688、下限为281,第8行RT=876超出上限,因此RT_filtered会被设为NA,其余行RT均在区间内,将保留原值。

内容的提问来源于stack exchange,提问作者Mikel Jimenez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 05:02:15