How do I convert a wide dataframe to a long dataframe for a multilevel structure with 'quadruple nesting'?

前端 未结 1 1644
挽巷
挽巷 2020-12-07 01:38

I conducted a study that, in retrospect (one lives, one learns :-)) appears to generate multilevel data. Now I\'m trying to restructure the dataset from wide to long so that

相关标签:
1条回答
  • 2020-12-07 02:21

    I think your problem can be solved with a two step approach:

    1. melt your data into a long data.frame (or as I did, in a long data.table)
    2. split the variable column with all the labels into separate columns for each required grouping variable.

    As the information for this is in the labels, this can easily be achieved with the tstrsplit function from the data.table package.

    This is what you might be looking for:

    library(data.table)
    longData <- melt(setDT(wideData), id.vars="id")
    longData[, c("moment", "intervention", "number", "behavior") := 
                    tstrsplit(variable, "_", type.convert = TRUE)
           ][, variable:=NULL]
    

    the result:

    > head(longData,15)
        id       value moment intervention number behavior
     1:  1 -0.07747254     t0         fear      1     diet
     2:  2 -0.76207379     t0         fear      1     diet
     3:  3  1.15501244     t0         fear      1     diet
     4:  4  1.24792369     t0         fear      1     diet
     5:  5 -0.28226121     t0         fear      1     diet
     6:  1 -1.04875354     t1         fear      1     diet
     7:  2 -0.91436882     t1         fear      1     diet
     8:  3  0.72863487     t1         fear      1     diet
     9:  4  0.10934261     t1         fear      1     diet
    10:  5 -0.06093002     t1         fear      1     diet
    11:  1 -0.70725760     t0         know      1     diet
    12:  2  1.06309003     t0         know      1     diet
    13:  3  0.89501164     t0         know      1     diet
    14:  4  1.48148316     t0         know      1     diet
    15:  5  0.22086835     t0         know      1     diet
    

    As an alternative to data.table, you can also split the variable column with the cSplit function of the splitstackshape package (you will have to rename the resulting variable columns afterwards though):

    library(splitstackshape)
    longData <- cSplit(longData, sep="_", "variable", "wide", type.convert=TRUE)
    names(longData) <- c("id","value","moment","intervention","number","behavior")
    

    or with tidyr:

    library(tidyr)
    separate(longData, variable, c("moment", "intervention", "number", "behavior"), sep="_", remove=TRUE)
    
    0 讨论(0)
提交回复
热议问题