gpt4 book ai didi

r - 如何将 span bin 信息转换为单独的 bins?

转载 作者:行者123 更新时间:2023-12-04 19:58:27 27 4
gpt4 key购买 nike

我进行了一项眼动追踪实验,试图检测两个区域(感兴趣区域,名为“代理”和“患者”的 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序列,然后从tidyverseunnest完成dataframe。如果您愿意,最后一行只是删除 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

关于r - 如何将 span bin 信息转换为单独的 bins?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56719373/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com