Reshape in R without aggregation (for example MTurk response strings)

后端 未结 4 690
旧巷少年郎
旧巷少年郎 2021-01-21 09:16

Ordinarily, I\'d use a pretty basic long-to-wide reshape for this, but it seems to be dropping my aggregation variables. The setup is I had a job on mechanical Turk that I perfo

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-21 09:57

    Using plyr:

    res = ddply(dat,.(Input.id,Input.State),
                function(x)unlist(as.character(x$Answer.Q1thing)))
    setNames(res,c('Id','State','Answer1','Answer2','Answer3'))
      Id State Answer1 Answer2  Answer3
    1 134231    NY Myguess Myguess BadGuess
    2 134812    CA Another Another  Another
    

    EDIT

    In case you have fewer than 3 answers:

    res = ddply(dat,.(Input.id,Input.State),
                function(x)
                  {
                  xx= unlist(as.character(x$Answer.Q1thing))
                  if(length(xx)==3)xx
                  else c(xx,rep(NA,3-length(xx)))
                })
    

提交回复
热议问题