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

后端 未结 4 805
旧巷少年郎
旧巷少年郎 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条回答
  •  感动是毒
    2021-01-21 10:08

    That was super helpful, @Ricardo and @agstudy. I realized my reshape wasn't working only because it demanded a unique, categorical "timevar". I think in most cases, you do have a categorical label/factor like this that makes it easy, but it is not much harder to count them, and make the count into a label.

    I had a second problem was that my number of answers were not consistent; you both gave good help for that, but I was also able to just generate a counter and then implemented my original long-to-wide.

    Where the count was < 3, that is where an Input.id only had 2 Answers, I got NAs for this, which is what I wanted.

    So altogether:

    mturk$idx <- with(mturk, ave(Input.id, Input.id, FUN=seq_along)) # weird!
    dat <- reshape(mturk, timevar="idx", idvar=c("Input.id", "Input.state"), direction="wide")
    

    I used the syntax for counting sequences within a group that I found here. This was a little idiosyncratic in the use of the ave() function, but seems to crop up in a couple other answers. Tried rtl, too, but had no luck. Using ave(x,x,seq_along) seems to mostly be a hack to avoid sorting. It's odd to use this work-around for sequences in groups because clearly both count() and rtl() are effectively creating this sequencing under the hood in a temp variable.

    I like the way data.table allows this sequencing better.

提交回复
热议问题