Function to convert data given in long format to a wide format.

to.wide(data, study, grp, ref, grpvars, postfix=c(".1",".2"),
        addid=TRUE, addcomp=TRUE, adddesign=TRUE, minlen=2,
        var.names=c("id","comp","design"))

Arguments

data

a data frame in long format.

study

either the name (given as a character string) or the position (given as a single number) of the study variable in the data frame.

grp

either the name (given as a character string) or the position (given as a single number) of the group variable in the data frame.

ref

optional character string to specify the reference group (must be one of the groups in the group variable). If not given, the most frequently occurring group is used as the reference group.

grpvars

either the names (given as a character vector) or the positions (given as a numeric vector) of the group-level variables.

postfix

a character string of length 2 giving the affix that is placed after the names of the group-level variables for the first and second group.

addid

logical to specify whether a row id variable should be added to the data frame (the default is TRUE).

addcomp

logical to specify whether a comparison id variable should be added to the data frame (the default is TRUE).

adddesign

logical to specify whether a design id variable should be added to the data frame (the default is TRUE).

minlen

integer to specify the minimum length of the shortened group names for the comparison and design id variables (the default is 2).

var.names

character vector with three elements to specify the name of the id, comparison, and design variables (the defaults are "id", "comp", and "design", respectively).

Details

A meta-analytic dataset may be structured in a ‘long’ format, where each row in the dataset corresponds to a particular study group (e.g., treatment arm). Using this function, such a dataset can be restructured into a ‘wide’ format, where each group within a study is contrasted against a particular reference group.

The study and group arguments are used to specify the study and group variables in the dataset (either as character strings or as numbers indicating the column positions of these variables in the dataset). Optional argument ref is used to specify the reference group (this must be one of the groups in the group variable). Argument grpvars is used to specify (either as a character vector or by giving the column positions) of those variables in the dataset that correspond to group-level outcomes (the remaining variables are treated as study-level outcomes).

The dataset is restructured so that a two-group study will yield a single row in the restructured dataset, contrasting the first group against the second/reference group. For studies with more than two groups (often called ‘multiarm’ or ‘multitreatment’ studies in the medical literature), the reference group is repeated as many times as needed (so a three-group study would yield two rows in the restructured dataset, contrasting two groups against a common reference group).

If a study does not include the reference group, then another group from the study will be used as the reference group. This group is chosen based on the factor levels of the grp variable (i.e., the last level that occurs in the study becomes the reference group).

To distinguish the names of the group-level outcome variables for the two first and second group in the restructured dataset, the strings given for the postfix argument are placed after the respective variable names.

If requested, row id, comparison id, and design id variables are added to the restructured dataset. The row id is simply a unique number for each row in the dataset. The comparison id variable indicates which two groups have been compared against each other). The design id variable indicates which groups were included in a particular study. The group names are shortened for the comparison and design variables (to at least minlen; the actual length might be longer to ensure uniqueness of the group names).

The examples below illustrate the use of this function.

Value

A data frame with rows contrasting groups against a reference group and an appropriate number of columns (depending on the number of group-level outcome variables).

References

Viechtbauer, W. (2010). Conducting meta-analyses in R with the metafor package. Journal of Statistical Software, 36(3), 1–48. https://doi.org/10.18637/jss.v036.i03

See also

contrmat for a function to construct a contrast matrix based on a dataset in wide format.

dat.hasselblad1998, dat.lopez2019, dat.obrien2003, dat.pagliaro1992, dat.senn2013 for illustrative examples.

Examples

### data in long format
dat <- dat.senn2013
dat <- dat[c(1,4,3,2,5,6)]
dat
#>                    study comment     treatment  ni    mi   sdi
#> 1       De Fronzo (1995)  change     metformin 213 -1.70 1.459
#> 2       De Fronzo (1995)  change       placebo 209  0.20 1.446
#> 3           Lewin (2007)  change     metformin 431 -0.74 1.106
#> 4           Lewin (2007)  change       placebo 144  0.08 1.004
#> 5          Willms (1999)  change     metformin  29 -2.50 0.862
#> 6          Willms (1999)  change      acarbose  31 -2.30 1.782
#> 7          Willms (1999)  change       placebo  29 -1.30 1.831
#> 8        Davidson (2007)  change rosiglitazone 117 -1.20 1.097
#> 9        Davidson (2007)  change       placebo 116  0.14 1.093
#> 10  Wolffenbuttel (1999)  change rosiglitazone 183 -0.90 1.100
#> 11  Wolffenbuttel (1999)  change       placebo 192  0.20 1.110
#> 12         Kipnes (2001)  change  pioglitazone 182 -1.20 1.369
#> 13         Kipnes (2001)  change       placebo 181  0.10 1.024
#> 14        Kerenyi (2004)  change rosiglitazone 160 -0.91 0.990
#> 15        Kerenyi (2004)  change       placebo 154 -0.14 0.920
#> 16       Hanefeld (2004)     raw  pioglitazone 319  7.61 1.072
#> 17       Hanefeld (2004)     raw     metformin 320  7.45 1.073
#> 18         Derosa (2004)     raw  pioglitazone  45  6.80 0.800
#> 19         Derosa (2004)     raw rosiglitazone  42  6.70 0.900
#> 20          Baksi (2004)  change rosiglitazone 218 -1.20 1.112
#> 21          Baksi (2004)  change       placebo 233  0.10 1.036
#> 22     Rosenstock (2008)  change rosiglitazone  59 -1.17 1.229
#> 23     Rosenstock (2008)  change       placebo  57 -0.08 1.208
#> 24            Zhu (2003)  change rosiglitazone 210 -1.90 1.470
#> 25            Zhu (2003)  change       placebo 105 -0.40 1.300
#> 26           Yang (2003)  change rosiglitazone 102 -1.09 1.650
#> 27           Yang (2003)  change     metformin  96 -0.95 1.500
#> 28 Vongthavaravat (2002)  change rosiglitazone 164 -1.10 1.559
#> 29 Vongthavaravat (2002)  change  sulfonylurea 170  0.10 0.992
#> 30          Oyama (2008)  change      acarbose  41 -0.70 0.800
#> 31          Oyama (2008)  change  sulfonylurea  43 -0.30 0.600
#> 32          Costa (1997)  change      acarbose  36 -1.10 0.360
#> 33          Costa (1997)  change       placebo  29 -0.30 0.700
#> 34      Hermansen (2007)  change   sitagliptin 106 -0.30 0.940
#> 35      Hermansen (2007)  change       placebo 106  0.27 0.940
#> 36         Garber (2008)  change  vildagliptin 132 -0.63 1.034
#> 37         Garber (2008)  change       placebo 144  0.07 1.080
#> 38           Alex (1998)  change     metformin 291  0.13 1.428
#> 39           Alex (1998)  change  sulfonylurea 300  0.50 1.450
#> 40       Johnston (1994)  change      miglitol  68 -0.41 1.072
#> 41       Johnston (1994)  change       placebo  63  0.33 1.032
#> 42      Johnston (1998a)  change      miglitol  91 -0.43 0.954
#> 43      Johnston (1998a)  change       placebo  43  0.98 1.311
#> 44            Kim (2007)  change rosiglitazone  57 -1.10 1.341
#> 45            Kim (2007)  change     metformin  56 -1.10 1.139
#> 46      Johnston (1998b)  change      miglitol  49 -0.12 1.400
#> 47      Johnston (1998b)  change       placebo  34  0.56 1.166
#> 48 Gonzalez-Ortiz (2004)  change     metformin  34 -1.30 1.861
#> 49 Gonzalez-Ortiz (2004)  change       placebo  37 -0.90 1.803
#> 50         Stucci (1996)     raw    benfluorex  28  8.03 1.290
#> 51         Stucci (1996)     raw       placebo  30  8.26 1.350
#> 52         Moulin (2006)  change    benfluorex 161 -0.82 1.028
#> 53         Moulin (2006)  change       placebo 156  0.19 1.374

### restructure to wide format
dat <- to.wide(dat, study="study", grp="treatment", ref="placebo", grpvars=4:6)
dat
#>                    study comment   treatment.1 ni.1  mi.1 sdi.1   treatment.2 ni.2  mi.2 sdi.2 id
#> 1            Alex (1998)  change     metformin  291  0.13 1.428  sulfonylurea  300  0.50 1.450  1
#> 2           Baksi (2004)  change rosiglitazone  218 -1.20 1.112       placebo  233  0.10 1.036  2
#> 3           Costa (1997)  change      acarbose   36 -1.10 0.360       placebo   29 -0.30 0.700  3
#> 4        Davidson (2007)  change rosiglitazone  117 -1.20 1.097       placebo  116  0.14 1.093  4
#> 5       De Fronzo (1995)  change     metformin  213 -1.70 1.459       placebo  209  0.20 1.446  5
#> 6          Derosa (2004)     raw  pioglitazone   45  6.80 0.800 rosiglitazone   42  6.70 0.900  6
#> 7          Garber (2008)  change  vildagliptin  132 -0.63 1.034       placebo  144  0.07 1.080  7
#> 8  Gonzalez-Ortiz (2004)  change     metformin   34 -1.30 1.861       placebo   37 -0.90 1.803  8
#> 9        Hanefeld (2004)     raw     metformin  320  7.45 1.073  pioglitazone  319  7.61 1.072  9
#> 10      Hermansen (2007)  change   sitagliptin  106 -0.30 0.940       placebo  106  0.27 0.940 10
#> 11       Johnston (1994)  change      miglitol   68 -0.41 1.072       placebo   63  0.33 1.032 11
#> 12      Johnston (1998a)  change      miglitol   91 -0.43 0.954       placebo   43  0.98 1.311 12
#> 13      Johnston (1998b)  change      miglitol   49 -0.12 1.400       placebo   34  0.56 1.166 13
#> 14        Kerenyi (2004)  change rosiglitazone  160 -0.91 0.990       placebo  154 -0.14 0.920 14
#> 15            Kim (2007)  change     metformin   56 -1.10 1.139 rosiglitazone   57 -1.10 1.341 15
#> 16         Kipnes (2001)  change  pioglitazone  182 -1.20 1.369       placebo  181  0.10 1.024 16
#> 17          Lewin (2007)  change     metformin  431 -0.74 1.106       placebo  144  0.08 1.004 17
#> 18         Moulin (2006)  change    benfluorex  161 -0.82 1.028       placebo  156  0.19 1.374 18
#> 19          Oyama (2008)  change      acarbose   41 -0.70 0.800  sulfonylurea   43 -0.30 0.600 19
#> 20     Rosenstock (2008)  change rosiglitazone   59 -1.17 1.229       placebo   57 -0.08 1.208 20
#> 21         Stucci (1996)     raw    benfluorex   28  8.03 1.290       placebo   30  8.26 1.350 21
#> 22 Vongthavaravat (2002)  change rosiglitazone  164 -1.10 1.559  sulfonylurea  170  0.10 0.992 22
#> 23         Willms (1999)  change      acarbose   31 -2.30 1.782       placebo   29 -1.30 1.831 23
#> 24         Willms (1999)  change     metformin   29 -2.50 0.862       placebo   29 -1.30 1.831 24
#> 25  Wolffenbuttel (1999)  change rosiglitazone  183 -0.90 1.100       placebo  192  0.20 1.110 25
#> 26           Yang (2003)  change     metformin   96 -0.95 1.500 rosiglitazone  102 -1.09 1.650 26
#> 27            Zhu (2003)  change rosiglitazone  210 -1.90 1.470       placebo  105 -0.40 1.300 27
#>     comp   design
#> 1  me-su    me-su
#> 2  ro-pl    ro-pl
#> 3  ac-pl    ac-pl
#> 4  ro-pl    ro-pl
#> 5  me-pl    me-pl
#> 6  pi-ro    pi-ro
#> 7  vi-pl    vi-pl
#> 8  me-pl    me-pl
#> 9  me-pi    me-pi
#> 10 si-pl    si-pl
#> 11 mi-pl    mi-pl
#> 12 mi-pl    mi-pl
#> 13 mi-pl    mi-pl
#> 14 ro-pl    ro-pl
#> 15 me-ro    me-ro
#> 16 pi-pl    pi-pl
#> 17 me-pl    me-pl
#> 18 be-pl    be-pl
#> 19 ac-su    ac-su
#> 20 ro-pl    ro-pl
#> 21 be-pl    be-pl
#> 22 ro-su    ro-su
#> 23 ac-pl ac-me-pl
#> 24 me-pl ac-me-pl
#> 25 ro-pl    ro-pl
#> 26 me-ro    me-ro
#> 27 ro-pl    ro-pl

### data in long format
dat <- dat.hasselblad1998
dat
#>    id study            authors year            trt  xi   ni
#> 1   1     1        Reid et al. 1974     no_contact  75  731
#> 2   2     1        Reid et al. 1974 ind_counseling 363  714
#> 3   3     2    Cottraux et al. 1983     no_contact   9  140
#> 4   4     2    Cottraux et al. 1983 ind_counseling  23  140
#> 5   5     2    Cottraux et al. 1983 grp_counseling  10  138
#> 6   6     3       Slama et al. 1990     no_contact   2  106
#> 7   7     3       Slama et al. 1990 ind_counseling   9  205
#> 8   8     4    Jamrozik et al. 1984     no_contact  58  549
#> 9   9     4    Jamrozik et al. 1984 ind_counseling 237 1561
#> 10 10     5      Rabkin et al. 1984     no_contact   0   33
#> 11 11     5      Rabkin et al. 1984 ind_counseling   9   48
#> 12 12     6   Decker and Evans 1989      self_help  20   49
#> 13 13     6   Decker and Evans 1989 ind_counseling  16   43
#> 14 14     7    Richmond et al. 1986     no_contact   3  100
#> 15 15     7    Richmond et al. 1986 ind_counseling  31   98
#> 16 16     8              Leung 1991     no_contact   1   31
#> 17 17     8              Leung 1991 ind_counseling  26   95
#> 18 18     9  Mothersill et al. 1988      self_help  11   78
#> 19 19     9  Mothersill et al. 1988 ind_counseling  12   85
#> 20 20     9  Mothersill et al. 1988 grp_counseling  29  170
#> 21 21    10    Langford et al. 1983     no_contact   6   39
#> 22 22    10    Langford et al. 1983 ind_counseling  17   77
#> 23 23    11       Gritz et al. 1992     no_contact  79  702
#> 24 24    11       Gritz et al. 1992      self_help  77  694
#> 25 25    12    Campbell et al. 1986     no_contact  18  671
#> 26 26    12    Campbell et al. 1986      self_help  21  535
#> 27 27    13     Sanders et al. 1989     no_contact  64  642
#> 28 28    13     Sanders et al. 1989 ind_counseling 107  761
#> 29 29    14    Hilleman et al. 1993 ind_counseling  12   76
#> 30 30    14    Hilleman et al. 1993 grp_counseling  20   74
#> 31 31    15     Gillams et al. 1984 ind_counseling   9   55
#> 32 32    15     Gillams et al. 1984 grp_counseling   3   26
#> 33 33    16 Mogielnicki et al. 1986      self_help   7   66
#> 34 34    16 Mogielnicki et al. 1986 grp_counseling  32  127
#> 35 35    17        Page et al. 1986     no_contact   5   62
#> 36 36    17        Page et al. 1986 ind_counseling   8   90
#> 37 37    18    Vetter and Ford 1990     no_contact  20  234
#> 38 38    18    Vetter and Ford 1990 ind_counseling  34  237
#> 39 39    19  Williams and Hall 1988     no_contact   0   20
#> 40 40    19  Williams and Hall 1988 grp_counseling   9   20
#> 41 41    20    Pallonen et al. 1994     no_contact   8  116
#> 42 42    20    Pallonen et al. 1994      self_help  19  149
#> 43 43    21     Russell et al. 1983     no_contact  95 1107
#> 44 44    21     Russell et al. 1983 ind_counseling 143 1031
#> 45 45    22 Stewart and Rosser 1982     no_contact  15  187
#> 46 46    22 Stewart and Rosser 1982 ind_counseling  36  504
#> 47 47    23     Russell et al. 1979     no_contact  78  584
#> 48 48    23     Russell et al. 1979 ind_counseling  73  675
#> 49 49    24    Kendrick et al. 1995     no_contact  69 1177
#> 50 50    24    Kendrick et al. 1995 ind_counseling  54  888

### restructure to wide format
dat <- to.wide(dat, study="study", grp="trt", ref="no_contact", grpvars=6:7)
dat
#>    id study            authors year          trt.1 xi.1 ni.1          trt.2 xi.2 ni.2  comp   design
#> 1   1     1        Reid et al. 1974 ind_counseling  363  714     no_contact   75  731 in-no    in-no
#> 2   2     2    Cottraux et al. 1983 grp_counseling   10  138     no_contact    9  140 gr-no gr-in-no
#> 3   3     2    Cottraux et al. 1983 ind_counseling   23  140     no_contact    9  140 in-no gr-in-no
#> 4   4     3       Slama et al. 1990 ind_counseling    9  205     no_contact    2  106 in-no    in-no
#> 5   5     4    Jamrozik et al. 1984 ind_counseling  237 1561     no_contact   58  549 in-no    in-no
#> 6   6     5      Rabkin et al. 1984 ind_counseling    9   48     no_contact    0   33 in-no    in-no
#> 7   7     6   Decker and Evans 1989 ind_counseling   16   43      self_help   20   49 in-se    in-se
#> 8   8     7    Richmond et al. 1986 ind_counseling   31   98     no_contact    3  100 in-no    in-no
#> 9   9     8              Leung 1991 ind_counseling   26   95     no_contact    1   31 in-no    in-no
#> 10 10     9  Mothersill et al. 1988 grp_counseling   29  170      self_help   11   78 gr-se gr-in-se
#> 11 11     9  Mothersill et al. 1988 ind_counseling   12   85      self_help   11   78 in-se gr-in-se
#> 12 12    10    Langford et al. 1983 ind_counseling   17   77     no_contact    6   39 in-no    in-no
#> 13 13    11       Gritz et al. 1992      self_help   77  694     no_contact   79  702 se-no    se-no
#> 14 14    12    Campbell et al. 1986      self_help   21  535     no_contact   18  671 se-no    se-no
#> 15 15    13     Sanders et al. 1989 ind_counseling  107  761     no_contact   64  642 in-no    in-no
#> 16 16    14    Hilleman et al. 1993 grp_counseling   20   74 ind_counseling   12   76 gr-in    gr-in
#> 17 17    15     Gillams et al. 1984 grp_counseling    3   26 ind_counseling    9   55 gr-in    gr-in
#> 18 18    16 Mogielnicki et al. 1986 grp_counseling   32  127      self_help    7   66 gr-se    gr-se
#> 19 19    17        Page et al. 1986 ind_counseling    8   90     no_contact    5   62 in-no    in-no
#> 20 20    18    Vetter and Ford 1990 ind_counseling   34  237     no_contact   20  234 in-no    in-no
#> 21 21    19  Williams and Hall 1988 grp_counseling    9   20     no_contact    0   20 gr-no    gr-no
#> 22 22    20    Pallonen et al. 1994      self_help   19  149     no_contact    8  116 se-no    se-no
#> 23 23    21     Russell et al. 1983 ind_counseling  143 1031     no_contact   95 1107 in-no    in-no
#> 24 24    22 Stewart and Rosser 1982 ind_counseling   36  504     no_contact   15  187 in-no    in-no
#> 25 25    23     Russell et al. 1979 ind_counseling   73  675     no_contact   78  584 in-no    in-no
#> 26 26    24    Kendrick et al. 1995 ind_counseling   54  888     no_contact   69 1177 in-no    in-no