如何将跨度箱信息转换为单独的箱?

时间:2019-06-22 21:42:33

标签: r

我进行了一次眼动追踪实验,试图检测这段时间内两个区域(关注区域,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分成单独的容器,如下所示:

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
...

这意味着每一行都是一个bin。关于如何用R解决问题有什么建议或想法吗?

1 个答案:

答案 0 :(得分:0)

我们可以使用mapply来获取bin序列,然后使用unnest中的tidyverse来完成数据帧。如果需要,最后一行只会删除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