【问题标题】:How to transform span bin information into separate bins?如何将 span bin 信息转换为单独的 bin?
【发布时间】:2019-06-23 15:28:24
【问题描述】:

我进行了一项眼动追踪实验,试图检测两个区域(感兴趣区域,名为“代理”和“患者”的 AOI)的注视分布。我将整个时间划分为时间箱,并得到以下列表,例如:

Stimulus   Participant   AOI name   Start_bin    End_bin   span.bin
E19AP          P01         Patient      2           6          5
E19AP          P01         Agent        10          14         5
E19AP          P01         Agent        14          22         9
E19AP          P01         Agent        24          30         7
...

为了完成分析,我需要将 span.bin 拆分为单独的 bin,如下所示:

Stimulus   Participant   AOI name     bin
E19AP          P01         Patient     2
E19AP          P01         Patient     3
E19AP          P01         Patient     4
E19AP          P01         Patient     5
E19AP          P01         Patient     6
E19AP          P01         agent       10
E19AP          P01         agent       11
E19AP          P01         agent       12
E19AP          P01         agent       13
E19AP          P01         agent       14
...

这意味着每一行都是一个箱子。关于如何用 R 解决问题的任何建议或想法?

【问题讨论】:

  • 请将您的代码以文本而不是图像的形式放回原处(您最初使用的方式更好)。
  • 没问题!谢谢!

标签: r


【解决方案1】:

我们可以使用mapply得到bin序列,然后unnesttidyverse完成数据帧。如果您愿意,最后一行将删除 Start_BinEnd_Bin 列。

library(tidyverse)

Stimulus <- c("E19AP","E19AP","E19AP","E19AP","E19AP")
Participant <- c("P01","P01","P01","P01","P01")
AOI_Name <- c("patient","agent","agent","agent","agent")
Start_Bin <- c(2,10,14,24,31)
End_Bin <- c(6,14,22,30,33)
span.Bin <- c(5,5,9,7,3)
df <- data.frame(Stimulus,Participant,AOI_Name,Start_Bin,End_Bin)

df$Bin <- mapply(":",df$Start_Bin,df$End_Bin)
df <- unnest(df,Bin)
df <- df[,-c(4,5,6)]

> df[1:10,]

   Stimulus Participant AOI_Name Bin
1     E19AP         P01  patient   2
2     E19AP         P01  patient   3
3     E19AP         P01  patient   4
4     E19AP         P01  patient   5
5     E19AP         P01  patient   6
6     E19AP         P01    agent  10
7     E19AP         P01    agent  11
8     E19AP         P01    agent  12
9     E19AP         P01    agent  13
10    E19AP         P01    agent  14

【讨论】:

    猜你喜欢
    • 2020-07-03
    • 1970-01-01
    • 2015-03-20
    • 2013-12-24
    • 1970-01-01
    • 2011-09-29
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多