Gather multiple sets of columns

后端 未结 5 954
耶瑟儿~
耶瑟儿~ 2020-11-22 03:01

I have data from an online survey where respondents go through a loop of questions 1-3 times. The survey software (Qualtrics) records this data in multiple columns—that is,

5条回答
  •  迷失自我
    2020-11-22 03:28

    It's not at all related to "tidyr" and "dplyr", but here's another option to consider: merged.stack from my "splitstackshape" package, V1.4.0 and above.

    library(splitstackshape)
    merged.stack(df, id.vars = c("id", "time"), 
                 var.stubs = c("Q3.2.", "Q3.3."),
                 sep = "var.stubs")
    #     id       time .time_1       Q3.2.       Q3.3.
    #  1:  1 2009-01-01      1. -0.62645381  1.35867955
    #  2:  1 2009-01-01      2.  1.51178117 -0.16452360
    #  3:  1 2009-01-01      3.  0.91897737  0.39810588
    #  4:  2 2009-01-02      1.  0.18364332 -0.10278773
    #  5:  2 2009-01-02      2.  0.38984324 -0.25336168
    #  6:  2 2009-01-02      3.  0.78213630 -0.61202639
    #  7:  3 2009-01-03      1. -0.83562861  0.38767161
    # <<:::SNIP:::>>
    # 24:  8 2009-01-08      3. -1.47075238 -1.04413463
    # 25:  9 2009-01-09      1.  0.57578135  1.10002537
    # 26:  9 2009-01-09      2.  0.82122120 -0.11234621
    # 27:  9 2009-01-09      3. -0.47815006  0.56971963
    # 28: 10 2009-01-10      1. -0.30538839  0.76317575
    # 29: 10 2009-01-10      2.  0.59390132  0.88110773
    # 30: 10 2009-01-10      3.  0.41794156 -0.13505460
    #     id       time .time_1       Q3.2.       Q3.3.
    

提交回复
热议问题