R 用基于类别的多项选择答案分析多项选择题

R 用基于类别的多项选择答案分析多项选择题,r,data-manipulation,survey,data-management,R,Data Manipulation,Survey,Data Management,我有一个像这样的数据框 Country <- rep(c("Austria", "Austria","Belgium", "Belgium", "Spain", "Slovenia", "France"), times=3) Institute <- rep(c("Inst 1","Inst 2","Inst 3","Inst 4","Inst 5","Inst 6","Inst 7"), times=3) Ans <- rep(c(1,2,3,1,NA,

我有一个像这样的数据框

Country    <- rep(c("Austria", "Austria","Belgium", "Belgium", "Spain", "Slovenia", "France"), times=3)
Institute  <- rep(c("Inst 1","Inst 2","Inst 3","Inst 4","Inst 5","Inst 6","Inst 7"), times=3)
Ans        <- rep(c(1,2,3,1,NA,2,2),times=3)
Category.1 <- rep(c("Cat 1", "Cat 2", "Cat 2", "Cat 2","Cat 2", "Cat 1", "Cat 1"),times=3)
Category.2 <- rep(c("P", "L", "M", "P", "P", "L", "M"),times=3)
qs  <- c(rep("Q1.a-Some Text", times=7),rep("Q1.b-Some Text", times=7), rep("Q1.c-Some Text", times=7))    
df <- data.frame(Country=Country,Institute=Institute, Category.1=Category.1, Category.2=Category.2, qs=qs, Ans=Ans)
df<-df %>% spread(qs,Ans)
head(df)

 Country Institute Category.1 Category.2 Q1.a-Some Text Q1.b-Some Text Q1.c-Some Text
1  Austria    Inst 1      Cat 1          P              1              1              1
2  Austria    Inst 2      Cat 2          L              2              2              2
3  Belgium    Inst 3      Cat 2          M              3              3              3
4  Belgium    Inst 4      Cat 2          P              1              1              1
5   France    Inst 7      Cat 1          M              2              2              2
6 Slovenia    Inst 6      Cat 1          L              2              2              2

Country我认为您可以通过将数据保存为长格式(即不要执行
df%排列(qs,Ans)
)和使用
dplyr
,使您的代码更加灵活,例如:

本部分基本上再现了
多选功能的功能:

multichoice<-function(data, question.prefix){
  index<-grep(question.prefix, names(data))    # identifies the index for the available options in Q.12
  cases<-length(index)                # The number of possible options / columns 

  # Identify the range of possible answers for each question 
  # Step 1. Search for the min in each col and across each col choose the min
  # step 2. Search for the max in each col and across each col choose the max 

  mn<-min(data[,index[1:cases]], na.rm=T)
  mx<-max(data[,index[1:cases]], na.rm=T)
  d = colSums(data[, index] != 0, na.rm = TRUE)  # The number of elements across column vector, that are different from zero. 

  vec<-matrix(,nrow=length(mn:mx),ncol=cases)

  for(j in 1:cases){
    for(i in mn:mx){
      vec[i,j]=sum(data[, index[j]] == i, na.rm = TRUE)/d[j]  # This stores the relative responses for option j for the answer that is i
    }
  }

  vec1<-as.data.frame(vec)
  names(vec1)<-names(data[index])
  vec1<-t(vec1)
  return(vec1)
}
df %>% 
    group_by(qs,Ans) %>% 
    summarize(total=n()) %>% 
    filter(!is.na(Ans)) %>% 
    mutate(frac=total/sum(total)) %>% 
    dcast(qs~Ans,value.var='frac')
#               qs         1   2         3
# 1 Q1.a-Some Text 0.3333333 0.5 0.1666667
# 2 Q1.b-Some Text 0.3333333 0.5 0.1666667
# 3 Q1.c-Some Text 0.3333333 0.5 0.1666667
这一个给出了一个例子,如何修改它来考虑类别

df %>% 
    group_by(qs,Category.1,Ans) %>% 
    summarize(total=n()) %>% 
    filter(!is.na(Ans)) %>% 
    mutate(frac=total/sum(total)) %>% 
    dcast(qs~Ans+Category.1,value.var='frac')
#               qs   1_Cat 1   1_Cat 2   2_Cat 1   2_Cat 2   3_Cat 2
# 1 Q1.a-Some Text 0.3333333 0.3333333 0.6666667 0.3333333 0.3333333
# 2 Q1.b-Some Text 0.3333333 0.3333333 0.6666667 0.3333333 0.3333333
# 3 Q1.c-Some Text 0.3333333 0.3333333 0.6666667 0.3333333 0.3333333

这真的很酷,但我对如何阅读第二张表格有点困惑。例如,在第一个例子中,很明显,行加起来必须是100%,并且解释是立即的——Q1的0.33%。a回答为1,50%回答为2etc。然而,关于第二个表,我有点不知所措。在标题中,下划线前的值显示问题编号,下一个值显示类别。如果您展示了一个预期输出的示例,我可以帮助您处理您的表Hank you,基本上我希望我的表看起来是这样的,行最多100%,列最多100%,这取决于什么更方便,如果需要根据不同的类别拆分表,那么也可以。例如,在第二个表中,如果将响应的第一列与第三列相加,则会出现这种情况,因为这显示了该类别如何响应选项a,b和c加起来确实是100%。你能帮我看一下第二组结果中的表格吗?也就是说,当一个人考虑到类别时,以某种方式看,列是按照类别集合排列的。因此,我想首先让1类1、2类1、1类2、2类2等更改
groupby
中的订单,即
groupby(qs、Ans、Category.1)
df %>% 
    group_by(qs,Category.1,Ans) %>% 
    summarize(total=n()) %>% 
    filter(!is.na(Ans)) %>% 
    mutate(frac=total/sum(total)) %>% 
    dcast(qs~Ans+Category.1,value.var='frac')
#               qs   1_Cat 1   1_Cat 2   2_Cat 1   2_Cat 2   3_Cat 2
# 1 Q1.a-Some Text 0.3333333 0.3333333 0.6666667 0.3333333 0.3333333
# 2 Q1.b-Some Text 0.3333333 0.3333333 0.6666667 0.3333333 0.3333333
# 3 Q1.c-Some Text 0.3333333 0.3333333 0.6666667 0.3333333 0.3333333