【发布时间】:2017-12-06 17:44:33
【问题描述】:
总结:我正在分析发生的刺激 (A&B) 与用户可能的反应之间的时间差。
数据集的结构如下:
structure(list(User = c("005b98f3-5b1b-4d10-bdea-a55d012b2844",
"005b98f3-5b1b-4d10-bdea-a55d012b2844", "005b98f3-5b1b-4d10-bdea-a55d012b2844",
"005b98f3-5b1b-4d10-bdea-a55d012b2844", "005b98f3-5b1b-4d10-bdea-a55d012b2844",
"005b98f3-5b1b-4d10-bdea-a55d012b2844", "005b98f3-5b1b-4d10-bdea-a55d012b2844",
"005b98f3-5b1b-4d10-bdea-a55d012b2844", "005b98f3-5b1b-4d10-bdea-a55d012b2844",
"005b98f3-5b1b-4d10-bdea-a55d012b2844", "005b98f3-5b1b-4d10-bdea-a55d012b2844",
"005b98f3-5b1b-4d10-bdea-a55d012b2844", "005b98f3-5b1b-4d10-bdea-a55d012b2844",
"005b98f3-5b1b-4d10-bdea-a55d012b2844", "005b98f3-5b1b-4d10-bdea-a55d012b2844",
"005b98f3-5b1b-4d10-bdea-a55d012b2844", "005b98f3-5b1b-4d10-bdea-a55d012b2844",
"005b98f3-5b1b-4d10-bdea-a55d012b2844", "005b98f3-5b1b-4d10-bdea-a55d012b2844",
"005b98f3-5b1b-4d10-bdea-a55d012b2844"), Date = c("25.11.2015 13:59",
"03.12.2015 09:32", "07.12.2015 08:18", "08.12.2015 19:40", "08.12.2015 19:40",
"22.12.2015 08:52", "22.12.2015 08:50", "22.12.2015 15:42", "22.12.2015 20:46",
"05.01.2016 11:33", "05.01.2016 11:35", "05.01.2016 13:22", "05.01.2016 13:21",
"05.01.2016 13:22", "06.01.2016 09:18", "14.02.2016 22:47", "20.02.2016 21:27",
"01.04.2016 13:52", "24.07.2016 07:03", "04.08.2016 08:25"),
Hour = c(1645L, 1833L, 1928L, 1963L, 1963L, 2288L, 2288L,
2295L, 2300L, 2627L, 2627L, 2629L, 2629L, 2629L, 2649L, 3598L,
3741L, 4717L, 7447L, 7712L), StimuliA = c(1L, 0L, 1L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L,
0L), StimuliB = c(0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L,
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L), Responses = c(0L,
1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L,
1L, 1L, 1L, 0L)), .Names = c("User", "Date", "Hour", "StimuliA",
"StimuliB", "Responses"), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
关于数据的附加信息:数据表中的每一行都是一个事件日志,用户可以在其中感知特定刺激或执行操作(响应)。小时:从项目开始到事件发生时的“小时”。
目标:总体目标是测量刺激和反应之间的时间。 (如果有的话)我想为每个用户创建一个遍历数据集的循环,如果刺激的值为 1,它会检查以后是否有用户的响应并创建一个带有值的向量A 一个,B 一个。
问题: 我是否会使用 for 循环来执行此操作,该循环遍历每个用户并检查感知到的刺激,如果存在值 1,则检查相同的用户 ID 在最近的响应中是否具有值 1,然后比较 2 个日期?
子问题 // 我正在努力解决的问题
- 我实际上如何循环遍历每一行并检查它的条件语句,如果 TRUE 则执行命令? (如果别的?)。
- 然后我将如何作为命令保存该行中其他单元格的值?
- 然后告诉 R 查找相同用户 ID(按时间顺序)的最接近的响应并计算这 2 个值之间的时间差?
- 最终使用这些计算值创建一个向量
想要的结果:
Stimuli A c=(11253, 2122, 56969), Stimuli B c=(19512,107)
到目前为止,我自己编写的代码并不是很有帮助。我正在试验 for 循环和 if 语句,还有 ifelse 函数。
我是 R 的新手,但在 datacamp 上做了多个课程,但我仍然在努力将它应用到我自己的硕士论文工作中。感谢大家的帮助。
附加信息:
R version 3.4.0 (2017-04-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
【问题讨论】:
-
您的预期结果是什么?
-
@PLapointe 预期结果将是一个向量,其中包含刺激 A 和 B 的经过时间,直到在小时:分钟:秒内发生响应。 F.e 看到人们在感知刺激 A 后反应更快。
-
我的意思是,使用您提供的数据,您想要的结果是什么。告诉我们您期望的向量。
-
我们无法使用您的数据,因为刺激和反应不是针对同一用户的。
-
这看起来像是 SQL 的工作。
标签: r loops time conditional