作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我进行了一项眼动追踪实验,试图检测两个区域(感兴趣区域,名为“代理”和“患者”的 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 解决问题的任何建议或想法?
最佳答案
我们可以使用mapply
获取bin序列,然后从tidyverse
unnest
完成dataframe。如果您愿意,最后一行只是删除 Start_Bin
和 End_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
关于r - 如何将 span bin 信息转换为单独的 bins?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56719373/
我是一名优秀的程序员,十分优秀!