R 通过比较列中的答案找到多个“切换点”

R 通过比较列中的答案找到多个“切换点”,r,R,我有一个数据集,受试者在13个不同的B中选择a和B。下面是54名受试者和5个选项数据的简化示例。1是A,2是B subject choice1 choice2 choice3 choice4 choice5 1 1 1 1 1 2 2 2 2 1 1 2 2 2 3 3 1 2

我有一个数据集,受试者在13个不同的B中选择a和B。下面是54名受试者和5个选项数据的简化示例。1是A,2是B

      subject choice1 choice2 choice3 choice4 choice5
1       1       1       1       1       2       2       
2       2       1       1       2       2       2       
3       3       1       2       1       2       2       
4       4       1       2       2       2       2       
我想找出受试者将选项A切换到选项B的问题,即对于受试者1,这将是选项4

在之前的一项研究中,我们通过计算受试者选择选项a的次数,然后选择相应的选项B形成一个单独的矩阵来实现这一点。请参阅下面的代码

然而,现在的区别是,不是选择一个切换点,而是以随机顺序向受试者提问,因此有可能有多个切换点。例如,在上表中,受试者3在选项2切换到B,在选项4再次切换到B

我想找出第一次被试切换到选项B,以及最后一次在剩下的选项中坚持使用B之前的情况

sure_amounts <- matrix(nrow = 4, ncol = 13) # 4 treatments, 13 questions
sure_amounts[1, ] <- c(0, 2, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 8, 10)  # Option B's
sure_amounts[2, ] <- seq(2, 14, 1)
sure_amounts[3, ] <- seq(2, 14, 1)  
sure_amounts[4, ] <- seq(2, 14, 1)

b_choice <- matrix(nrow = 201, ncol = 4)
switch_choice <- matrix(nrow = 201, ncol = 4) # switching point form A to B

for(j in 1:4){    # number of treatments
  for(i in 201){   # number of subjects
    choice = NULL
    fl = data$ID == i
    k = 1 + 36*(j-1)      # 36 before going to the next treatment (due to other questions)
    
    choice = c(data[fl,k:(k+12)])
    b_choice[i,j] = length(choice[choice==1])  
    temp = b_choice[i,j]
    switch_choice[i,j] <- ifelse(temp==0, 0, sure_amounts[j, temp])
  }
}

有人对如何处理这个问题有什么建议吗?提前谢谢

我不确定您希望预期的输出是什么样子,但您可以尝试获取长格式的数据,并为每个主题选择从1->2切换的行

library(dplyr)

df %>%
  tidyr::pivot_longer(cols = -subject) %>%
  group_by(subject) %>%
  filter(value == 2 & lag(value) == 1 | 
         value == 1 & lead(value) == 2)

#    subject name    value
#     <int> <chr>   <int>
# 1       1 choice3     1
# 2       1 choice4     2
# 3       2 choice2     1
# 4       2 choice3     2
# 5       3 choice1     1
# 6       3 choice2     2
# 7       3 choice3     1
# 8       3 choice4     2
# 9       4 choice1     1
#10       4 choice2     2
在这里,我们可以看到主题1从1->2从选项3->选项4移动,依此类推

资料

基本R解决方案:

本质上,该代码只减去决策的滞后,并检测差异是否不等于零

代码:

数据:


另一种办法:

图书馆弹琴 图书馆长 图书馆咕噜声 df%>% mutateg=粘贴0选择1,选择2,选择3,选择4,选择5, switches=as.charactermapg、~pluckstr\u locate\u all.x、12、1%>% 选择-g >主题选择1选择2选择3选择4选择5开关 > 1 1 1 1 1 2 2 3:4 > 2 2 1 1 2 2 2 2:3 >312C1,3,2,4 > 4 4 1 2 2 2 2 1:2 数据 由v0.3.0于2020年7月10日创建

df <- structure(list(subject = 1:4, choice1 = c(1L, 1L, 1L, 1L), choice2 = c(1L, 
1L, 2L, 2L), choice3 = c(1L, 2L, 1L, 2L), choice4 = c(2L, 2L, 
2L, 2L), choice5 = c(2L, 2L, 2L, 2L)), class = "data.frame", 
row.names = c(NA, -4L))
lapply(as.data.frame(t(df_1)[-1,]), function(x){
  t <- x - c(x[-1], 0) # row substracted by shortened row
  z <- which(t[-length(t)] != 0) # values not equal to zero and rm last value 
  z + 1 # remove lag
})

# $`1`
# [1] 4

# $`2`
# [1] 3

# $`3`
# [1] 2 3 4

# $`4`
# [1] 2

df_1 <- read.table(text = "      subject choice1 choice2 choice3 choice4 choice5
1       1       1       1       1       2       2       
2       2       1       1       2       2       2       
3       3       1       2       1       2       2       
4       4       1       2       2       2       2 ", header = T)
df <- structure(list(subject = 1:4, choice1 = c(1L, 1L, 1L, 1L), choice2 = c(1L, 
                                                                             1L, 2L, 2L), choice3 = c(1L, 2L, 1L, 2L), choice4 = c(2L, 2L, 
                                                                                                                                   2L, 2L), choice5 = c(2L, 2L, 2L, 2L)), class = "data.frame", row.names = c("1", 
                                                                                                                                                                                                              "2", "3", "4"))