【问题标题】:Summarize and count data in R with dplyr使用 dplyr 对 R 中的数据进行汇总和计数
【发布时间】:2017-07-17 14:37:23
【问题描述】:

目标:使用 dplyr 汇总/计算同一行中发生的刺激的反应。

背景:我在另一个主题上得到了一些极好的帮助:Loop through dataframe in R and measure time difference between two values

现在,我正在使用相同/相似的数据集,我的目标是计算用户对感知刺激的反应,这些反应与刺激发生的位置在同一行。数据集如下所示:

structure(list(User = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), StimuliA = c(1L, 0L, 
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L), StimuliB = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 
0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L), R2 = c(0L, 0L, 0L, 0L, 
0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 0L
), R3 = c(0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L), R4 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), R5 = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L), R6 = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L), R7 = c(0L, 1L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("User", 
"StimuliA", "StimuliB", "R2", "R3", "R4", "R5", "R6", "R7"), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -20L), spec = structure(list(
    cols = structure(list(User = structure(list(), class = c("collector_integer", 
    "collector")), StimuliA = structure(list(), class = c("collector_integer", 
    "collector")), StimuliB = structure(list(), class = c("collector_integer", 
    "collector")), R2 = structure(list(), class = c("collector_integer", 
    "collector")), R3 = structure(list(), class = c("collector_integer", 
    "collector")), R4 = structure(list(), class = c("collector_integer", 
    "collector")), R5 = structure(list(), class = c("collector_integer", 
    "collector")), R6 = structure(list(), class = c("collector_integer", 
    "collector")), R7 = structure(list(), class = c("collector_integer", 
    "collector"))), .Names = c("User", "StimuliA", "StimuliB", 
    "R2", "R3", "R4", "R5", "R6", "R7")), default = structure(list(), 
class = c("collector_guess", 
    "collector"))), .Names = c("cols", "default"), class = "col_spec"))

所需的输出:所需的输出将是汇总列表,其中所有响应聚合在发生的刺激的同一行中:

U   StimuliA    StimuliB    R2  R3  R4  R5  R6  R7
1      1            0       0   0   0   0   0   1
1      1            0       1   1   0   0   1   0
1      0            1       1   2   0   0   1   0
1      0            1       0   0   0   0   0   0
2      1            0       3   0   0   0   0   0
2      0            1       1   0   0   0   2   0

在示例中,第 1 行记录了 A 的刺激,第 2 行记录了 R7 的 1。然后,所需结果中的结果是一行,在 StimuliA 处为 1,在 R7 处为 1。然后它又开始了,因为在第 3 行我们有一个新的 1 表示 StimuliA。

最后,对于每个刺激,都会在同一行中汇总以下发生的响应 (R2-R7)。 Stimuli(A 或 B)的值保持为 1。

问题:我觉得我可以使用 dplyr 包来实现这一点,但我之前的尝试并没有得出太多有用的输出。我将如何使用 dplyr 命令构建语法,还是应该从另一个方向寻找解决方案?我会改变相同的现有数据框还是创建一个新数据框?

感谢所有输入和帮助!

【问题讨论】:

  • 在基础 R 中,您可以使用 aggregate(. ~ User + StimuliA + StimuliB, data=dat, sum)dplyr 语法中,也许是 dat %>% group_by(., User, StimuliA, StimuliB) %>% summarize_all(sum)
  • 这个问题不是很清楚,但据我了解,有一行有刺激,即 StimuliA 或 StimuliB 中的 1,然后是对该刺激的几个反应,其中 StimuliA 和 StimuliB是 0,但其他变量之一等于 1。我认为,问题是询问如何将刺激后的 n 行聚合到具有刺激的行。
  • df %>% group_by(User) %>% mutate(Sta = cumsum(StimuliA), Stb = cumsum(StimuliB)) %>% group_by(User, Sta, Stb) %>% summarise(StA = sum(StimuliA), StB = sum(StimuliB), R2 = sum(R2), R3 = sum(R3), R4 = sum(R4), R5 = sum(R5), R6 = sum(R6), R7 = sum(R7)) %>% select(-Sta, -Stb)
  • @Eumenedies 是的,对不起,我会更新这个问题。一旦发生刺激,无论是刺激 A 还是刺激 B,1,然后我想在同一行中总结/计算以下所有响应 R2-R7。
  • @Eumenedies 我更新了信息。不幸的是,我不完全理解您的解决方案。计算 StimuliA 的 cumsum 的原因是什么?

标签: r dplyr


【解决方案1】:

这是基于 R 的两行解决方案。首先,创建一个对每个用户-(新)刺激组合唯一的 ID。这是通过pastecumsum 完成的。

dat$stims <- with(dat, paste(cumsum(StimuliA), cumsum(StimuliB), sep="_"))

然后使用aggregate 计算每个新 ID 的响应

aggregate(. ~ User + stims, data=dat, sum)
  User stims StimuliA StimuliB R2 R3 R4 R5 R6 R7
1    1   1_0        1        0  0  0  0  0  0  1
2    1   2_0        1        0  1  1  0  0  1  0
3    1   2_1        0        1  1  2  0  0  1  0
4    1   2_2        0        1  0  0  0  0  0  0
5    2   3_2        1        0  3  0  0  0  0  0
6    2   3_3        0        1  1  0  0  0  2  0

【讨论】:

  • 后续问题:在原始数据集中,我有一个带有日期的列。当我尝试包含此列的方法时,R 给了我一个错误,因为它们是一个因素。我将如何转换此列中的值以使其与日期一起使用。我所需要的只是该行的刺激日期,其中响应(R2-R7)正在聚合。
  • 您不想使用日期作为因素。使用as.Date 将日期转换为日期变量(SO 上有很多关于此的帖子)。然后一种方法是按用户和类似于上面的刺激分别聚合日期变量,采用min 而不是sum。然后合并两个生成的data.frames。如果这没有意义,那么可能值得提出一个链接到该问题的 new 问题,并添加日期变量的附加问题。还包括一个包含此变量的示例数据集。
  • 我在这里发布了一个带有示例的新问题:stackoverflow.com/questions/45322102/…
猜你喜欢
  • 2017-05-31
  • 1970-01-01
  • 2015-06-01
  • 2016-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-01
  • 1970-01-01
相关资源
最近更新 更多