Function to convert summary data in vector format to the corresponding long format.

to.long(measure, ai, bi, ci, di, n1i, n2i, x1i, x2i, t1i, t2i,
        m1i, m2i, sd1i, sd2i, xi, mi, ri, ti, sdi, ni, data, slab, subset,
        add=1/2, to="none", drop00=FALSE, vlong=FALSE, append=TRUE, var.names)

Arguments

measure

a character string to specify the effect size or outcome measure corresponding to the summary data supplied. See ‘Details’ and the documentation of the escalc function for possible options.

ai

vector with the \(2 \times 2\) table frequencies (upper left cell).

bi

vector with the \(2 \times 2\) table frequencies (upper right cell).

ci

vector with the \(2 \times 2\) table frequencies (lower left cell).

di

vector with the \(2 \times 2\) table frequencies (lower right cell).

n1i

vector with the group sizes or row totals (first group/row).

n2i

vector with the group sizes or row totals (second group/row).

x1i

vector with the number of events (first group).

x2i

vector with the number of events (second group).

t1i

vector with the total person-times (first group).

t2i

vector with the total person-times (second group).

m1i

vector with the means (first group or time point).

m2i

vector with the means (second group or time point).

sd1i

vector with the standard deviations (first group or time point).

sd2i

vector with the standard deviations (second group or time point).

xi

vector with the frequencies of the event of interest.

mi

vector with the frequencies of the complement of the event of interest or the group means.

ri

vector with the raw correlation coefficients.

ti

vector with the total person-times.

sdi

vector with the standard deviations.

ni

vector with the sample/group sizes.

data

optional data frame containing the variables given to the arguments above.

slab

optional vector with labels for the studies.

subset

optional (logical or numeric) vector to specify the subset of studies that should included in the data frame returned by the function.

add

see the documentation of the escalc function.

to

see the documentation of the escalc function.

drop00

see the documentation of the escalc function.

vlong

optional logical whether a very long format should be used (only relevant for \(2 \times 2\) or \(1 \times 2\) table data).

append

logical to specify whether the data frame specified via the data argument (if one has been specified) should be returned together with the long format data (the default is TRUE). Can also be a character or numeric vector to specify which variables from data to append.

var.names

optional character vector with variable names (the length depends on the data type). If unspecified, the function sets appropriate variable names by default.

Details

The escalc function describes a wide variety of effect sizes or outcome measures that can be computed for a meta-analysis. The summary data used to compute those measures are typically contained in vectors, each element corresponding to a study. The to.long function takes this information and constructs a long format dataset from these data.

For example, in various fields (such as the health and medical sciences), the response variable measured is often dichotomous (binary), so that the data from a study comparing two different groups can be expressed in terms of a \(2 \times 2\) table, such as:

outcome 1outcome 2total
group 1aibin1i
group 2cidin2i

where ai, bi, ci, and di denote the cell frequencies (i.e., the number of individuals falling into a particular category) and n1i and n2i the row totals (i.e., the group sizes).

The cell frequencies in \(k\) such \(2 \times 2\) tables can be specified via the ai, bi, ci, and di arguments (or alternatively, via the ai, ci, n1i, and n2i arguments). The function then creates the corresponding long format dataset. The measure argument should then be set equal to one of the outcome measures that can be computed based on this type of data, such as "RR", "OR", "RD" (it is not relevant which specific measure is chosen, as long as it corresponds to the specified summary data). See the documentation of the escalc function for more details on the types of data formats available.

The long format for data of this type consists of two rows per study, a factor indicating the study (default name study), a dummy variable indicating the group (default name group, coded as 1 and 2), and two variables indicating the number of individuals experiencing outcome 1 or outcome 2 (default names out1 and out2). Alternatively, if vlong=TRUE, then the long format consists of four rows per study, a factor indicating the study (default name study), a dummy variable indicating the group (default name group, coded as 1 and 2), a dummy variable indicating the outcome (default name outcome, coded as 1 and 2), and a variable indicating the frequency of the respective outcome (default name freq).

The default variable names can be changed via the var.names argument (must be of the appropriate length, depending on the data type).

The examples below illustrate the use of this function.

Value

A data frame with either \(k\), \(2 \times k\), or \(4 \times k\) rows and an appropriate number of columns (depending on the data type) with the data in long format. If append=TRUE and a data frame was specified via the data argument, then the data in long format are appended to the original data frame (with rows repeated an appropriate number of times).

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

escalc for a function to compute observed effect sizes or outcomes (and corresponding sampling variances) based on similar inputs.

to.table for a function to turn similar inputs into tabular form.

Examples

### convert data to long format
dat.bcg
#>    trial               author year tpos  tneg cpos  cneg ablat      alloc
#> 1      1              Aronson 1948    4   119   11   128    44     random
#> 2      2     Ferguson & Simes 1949    6   300   29   274    55     random
#> 3      3      Rosenthal et al 1960    3   228   11   209    42     random
#> 4      4    Hart & Sutherland 1977   62 13536  248 12619    52     random
#> 5      5 Frimodt-Moller et al 1973   33  5036   47  5761    13  alternate
#> 6      6      Stein & Aronson 1953  180  1361  372  1079    44  alternate
#> 7      7     Vandiviere et al 1973    8  2537   10   619    19     random
#> 8      8           TPT Madras 1980  505 87886  499 87892    13     random
#> 9      9     Coetzee & Berjak 1968   29  7470   45  7232    27     random
#> 10    10      Rosenthal et al 1961   17  1699   65  1600    42 systematic
#> 11    11       Comstock et al 1974  186 50448  141 27197    18 systematic
#> 12    12   Comstock & Webster 1969    5  2493    3  2338    33 systematic
#> 13    13       Comstock et al 1976   27 16886   29 17825    33 systematic
dat.long <- to.long(measure="OR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg)
dat.long
#>    trial               author year tpos  tneg cpos  cneg ablat      alloc study group out1  out2
#> 1      1              Aronson 1948    4   119   11   128    44     random     1     1    4   119
#> 2      1              Aronson 1948    4   119   11   128    44     random     1     2   11   128
#> 3      2     Ferguson & Simes 1949    6   300   29   274    55     random     2     1    6   300
#> 4      2     Ferguson & Simes 1949    6   300   29   274    55     random     2     2   29   274
#> 5      3      Rosenthal et al 1960    3   228   11   209    42     random     3     1    3   228
#> 6      3      Rosenthal et al 1960    3   228   11   209    42     random     3     2   11   209
#> 7      4    Hart & Sutherland 1977   62 13536  248 12619    52     random     4     1   62 13536
#> 8      4    Hart & Sutherland 1977   62 13536  248 12619    52     random     4     2  248 12619
#> 9      5 Frimodt-Moller et al 1973   33  5036   47  5761    13  alternate     5     1   33  5036
#> 10     5 Frimodt-Moller et al 1973   33  5036   47  5761    13  alternate     5     2   47  5761
#> 11     6      Stein & Aronson 1953  180  1361  372  1079    44  alternate     6     1  180  1361
#> 12     6      Stein & Aronson 1953  180  1361  372  1079    44  alternate     6     2  372  1079
#> 13     7     Vandiviere et al 1973    8  2537   10   619    19     random     7     1    8  2537
#> 14     7     Vandiviere et al 1973    8  2537   10   619    19     random     7     2   10   619
#> 15     8           TPT Madras 1980  505 87886  499 87892    13     random     8     1  505 87886
#> 16     8           TPT Madras 1980  505 87886  499 87892    13     random     8     2  499 87892
#> 17     9     Coetzee & Berjak 1968   29  7470   45  7232    27     random     9     1   29  7470
#> 18     9     Coetzee & Berjak 1968   29  7470   45  7232    27     random     9     2   45  7232
#> 19    10      Rosenthal et al 1961   17  1699   65  1600    42 systematic    10     1   17  1699
#> 20    10      Rosenthal et al 1961   17  1699   65  1600    42 systematic    10     2   65  1600
#> 21    11       Comstock et al 1974  186 50448  141 27197    18 systematic    11     1  186 50448
#> 22    11       Comstock et al 1974  186 50448  141 27197    18 systematic    11     2  141 27197
#> 23    12   Comstock & Webster 1969    5  2493    3  2338    33 systematic    12     1    5  2493
#> 24    12   Comstock & Webster 1969    5  2493    3  2338    33 systematic    12     2    3  2338
#> 25    13       Comstock et al 1976   27 16886   29 17825    33 systematic    13     1   27 16886
#> 26    13       Comstock et al 1976   27 16886   29 17825    33 systematic    13     2   29 17825

### extra long format
dat <- to.long(measure="OR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg, vlong=TRUE)
dat
#>    trial               author year tpos  tneg cpos  cneg ablat      alloc study group outcome  freq
#> 1      1              Aronson 1948    4   119   11   128    44     random     1     1       1     4
#> 2      1              Aronson 1948    4   119   11   128    44     random     1     1       2   119
#> 3      1              Aronson 1948    4   119   11   128    44     random     1     2       1    11
#> 4      1              Aronson 1948    4   119   11   128    44     random     1     2       2   128
#> 5      2     Ferguson & Simes 1949    6   300   29   274    55     random     2     1       1     6
#> 6      2     Ferguson & Simes 1949    6   300   29   274    55     random     2     1       2   300
#> 7      2     Ferguson & Simes 1949    6   300   29   274    55     random     2     2       1    29
#> 8      2     Ferguson & Simes 1949    6   300   29   274    55     random     2     2       2   274
#> 9      3      Rosenthal et al 1960    3   228   11   209    42     random     3     1       1     3
#> 10     3      Rosenthal et al 1960    3   228   11   209    42     random     3     1       2   228
#> 11     3      Rosenthal et al 1960    3   228   11   209    42     random     3     2       1    11
#> 12     3      Rosenthal et al 1960    3   228   11   209    42     random     3     2       2   209
#> 13     4    Hart & Sutherland 1977   62 13536  248 12619    52     random     4     1       1    62
#> 14     4    Hart & Sutherland 1977   62 13536  248 12619    52     random     4     1       2 13536
#> 15     4    Hart & Sutherland 1977   62 13536  248 12619    52     random     4     2       1   248
#> 16     4    Hart & Sutherland 1977   62 13536  248 12619    52     random     4     2       2 12619
#> 17     5 Frimodt-Moller et al 1973   33  5036   47  5761    13  alternate     5     1       1    33
#> 18     5 Frimodt-Moller et al 1973   33  5036   47  5761    13  alternate     5     1       2  5036
#> 19     5 Frimodt-Moller et al 1973   33  5036   47  5761    13  alternate     5     2       1    47
#> 20     5 Frimodt-Moller et al 1973   33  5036   47  5761    13  alternate     5     2       2  5761
#> 21     6      Stein & Aronson 1953  180  1361  372  1079    44  alternate     6     1       1   180
#> 22     6      Stein & Aronson 1953  180  1361  372  1079    44  alternate     6     1       2  1361
#> 23     6      Stein & Aronson 1953  180  1361  372  1079    44  alternate     6     2       1   372
#> 24     6      Stein & Aronson 1953  180  1361  372  1079    44  alternate     6     2       2  1079
#> 25     7     Vandiviere et al 1973    8  2537   10   619    19     random     7     1       1     8
#> 26     7     Vandiviere et al 1973    8  2537   10   619    19     random     7     1       2  2537
#> 27     7     Vandiviere et al 1973    8  2537   10   619    19     random     7     2       1    10
#> 28     7     Vandiviere et al 1973    8  2537   10   619    19     random     7     2       2   619
#> 29     8           TPT Madras 1980  505 87886  499 87892    13     random     8     1       1   505
#> 30     8           TPT Madras 1980  505 87886  499 87892    13     random     8     1       2 87886
#> 31     8           TPT Madras 1980  505 87886  499 87892    13     random     8     2       1   499
#> 32     8           TPT Madras 1980  505 87886  499 87892    13     random     8     2       2 87892
#> 33     9     Coetzee & Berjak 1968   29  7470   45  7232    27     random     9     1       1    29
#> 34     9     Coetzee & Berjak 1968   29  7470   45  7232    27     random     9     1       2  7470
#> 35     9     Coetzee & Berjak 1968   29  7470   45  7232    27     random     9     2       1    45
#> 36     9     Coetzee & Berjak 1968   29  7470   45  7232    27     random     9     2       2  7232
#> 37    10      Rosenthal et al 1961   17  1699   65  1600    42 systematic    10     1       1    17
#> 38    10      Rosenthal et al 1961   17  1699   65  1600    42 systematic    10     1       2  1699
#> 39    10      Rosenthal et al 1961   17  1699   65  1600    42 systematic    10     2       1    65
#> 40    10      Rosenthal et al 1961   17  1699   65  1600    42 systematic    10     2       2  1600
#> 41    11       Comstock et al 1974  186 50448  141 27197    18 systematic    11     1       1   186
#> 42    11       Comstock et al 1974  186 50448  141 27197    18 systematic    11     1       2 50448
#> 43    11       Comstock et al 1974  186 50448  141 27197    18 systematic    11     2       1   141
#> 44    11       Comstock et al 1974  186 50448  141 27197    18 systematic    11     2       2 27197
#> 45    12   Comstock & Webster 1969    5  2493    3  2338    33 systematic    12     1       1     5
#> 46    12   Comstock & Webster 1969    5  2493    3  2338    33 systematic    12     1       2  2493
#> 47    12   Comstock & Webster 1969    5  2493    3  2338    33 systematic    12     2       1     3
#> 48    12   Comstock & Webster 1969    5  2493    3  2338    33 systematic    12     2       2  2338
#> 49    13       Comstock et al 1976   27 16886   29 17825    33 systematic    13     1       1    27
#> 50    13       Comstock et al 1976   27 16886   29 17825    33 systematic    13     1       2 16886
#> 51    13       Comstock et al 1976   27 16886   29 17825    33 systematic    13     2       1    29
#> 52    13       Comstock et al 1976   27 16886   29 17825    33 systematic    13     2       2 17825

### select variables to append
dat.long <- to.long(measure="OR", ai=tpos, bi=tneg, ci=cpos, di=cneg,
                    data=dat.bcg, append=c("author","year"))
dat.long
#>                  author year study group out1  out2
#> 1               Aronson 1948     1     1    4   119
#> 2               Aronson 1948     1     2   11   128
#> 3      Ferguson & Simes 1949     2     1    6   300
#> 4      Ferguson & Simes 1949     2     2   29   274
#> 5       Rosenthal et al 1960     3     1    3   228
#> 6       Rosenthal et al 1960     3     2   11   209
#> 7     Hart & Sutherland 1977     4     1   62 13536
#> 8     Hart & Sutherland 1977     4     2  248 12619
#> 9  Frimodt-Moller et al 1973     5     1   33  5036
#> 10 Frimodt-Moller et al 1973     5     2   47  5761
#> 11      Stein & Aronson 1953     6     1  180  1361
#> 12      Stein & Aronson 1953     6     2  372  1079
#> 13     Vandiviere et al 1973     7     1    8  2537
#> 14     Vandiviere et al 1973     7     2   10   619
#> 15           TPT Madras 1980     8     1  505 87886
#> 16           TPT Madras 1980     8     2  499 87892
#> 17     Coetzee & Berjak 1968     9     1   29  7470
#> 18     Coetzee & Berjak 1968     9     2   45  7232
#> 19      Rosenthal et al 1961    10     1   17  1699
#> 20      Rosenthal et al 1961    10     2   65  1600
#> 21       Comstock et al 1974    11     1  186 50448
#> 22       Comstock et al 1974    11     2  141 27197
#> 23   Comstock & Webster 1969    12     1    5  2493
#> 24   Comstock & Webster 1969    12     2    3  2338
#> 25       Comstock et al 1976    13     1   27 16886
#> 26       Comstock et al 1976    13     2   29 17825
dat.long <- to.long(measure="OR", ai=tpos, bi=tneg, ci=cpos, di=cneg,
                    data=dat.bcg, append=2:3)
dat.long
#>                  author year study group out1  out2
#> 1               Aronson 1948     1     1    4   119
#> 2               Aronson 1948     1     2   11   128
#> 3      Ferguson & Simes 1949     2     1    6   300
#> 4      Ferguson & Simes 1949     2     2   29   274
#> 5       Rosenthal et al 1960     3     1    3   228
#> 6       Rosenthal et al 1960     3     2   11   209
#> 7     Hart & Sutherland 1977     4     1   62 13536
#> 8     Hart & Sutherland 1977     4     2  248 12619
#> 9  Frimodt-Moller et al 1973     5     1   33  5036
#> 10 Frimodt-Moller et al 1973     5     2   47  5761
#> 11      Stein & Aronson 1953     6     1  180  1361
#> 12      Stein & Aronson 1953     6     2  372  1079
#> 13     Vandiviere et al 1973     7     1    8  2537
#> 14     Vandiviere et al 1973     7     2   10   619
#> 15           TPT Madras 1980     8     1  505 87886
#> 16           TPT Madras 1980     8     2  499 87892
#> 17     Coetzee & Berjak 1968     9     1   29  7470
#> 18     Coetzee & Berjak 1968     9     2   45  7232
#> 19      Rosenthal et al 1961    10     1   17  1699
#> 20      Rosenthal et al 1961    10     2   65  1600
#> 21       Comstock et al 1974    11     1  186 50448
#> 22       Comstock et al 1974    11     2  141 27197
#> 23   Comstock & Webster 1969    12     1    5  2493
#> 24   Comstock & Webster 1969    12     2    3  2338
#> 25       Comstock et al 1976    13     1   27 16886
#> 26       Comstock et al 1976    13     2   29 17825

### convert data to long format
dat.long <- to.long(measure="IRR", x1i=x1i, x2i=x2i, t1i=t1i, t2i=t2i,
                   data=dat.hart1999, var.names=c("id", "group", "events", "ptime"))
dat.long
#>    trial  study year x1i n1i t1i x2i n2i t2i compgrp  prevtype   trinr id group events ptime
#> 1      1 AFASAK 1989   9 335 413  19 336 398 placebo   primary 2.8-4.2  1     1      9   413
#> 2      1 AFASAK 1989   9 335 413  19 336 398 placebo   primary 2.8-4.2  1     2     19   398
#> 3      2   SPAF 1991   8 210 263  19 211 245 placebo   primary 2.0-4.5  2     1      8   263
#> 4      2   SPAF 1991   8 210 263  19 211 245 placebo   primary 2.0-4.5  2     2     19   245
#> 5      3 BAATAF 1990   3 212 487  13 208 435 control   primary 1.5-2.7  3     1      3   487
#> 6      3 BAATAF 1990   3 212 487  13 208 435 control   primary 1.5-2.7  3     2     13   435
#> 7      4   CAFA 1991   6 187 237   9 191 241 placebo   primary 2.0-3.0  4     1      6   237
#> 8      4   CAFA 1991   6 187 237   9 191 241 placebo   primary 2.0-3.0  4     2      9   241
#> 9      5 SPINAF 1992   7 281 489  23 290 483 placebo   primary 1.4-2.8  5     1      7   489
#> 10     5 SPINAF 1992   7 281 489  23 290 483 placebo   primary 1.4-2.8  5     2     23   483
#> 11     6   EAFT 1993  20 225 507  50 214 405 placebo secondary 2.5-4.0  6     1     20   507
#> 12     6   EAFT 1993  20 225 507  50 214 405 placebo secondary 2.5-4.0  6     2     50   405

### convert data to long format
dat.long <- to.long(measure="MD", m1i=m1i, sd1i=sd1i, n1i=n1i,
                    m2i=m2i, sd2i=sd2i, n2i=n2i, data=dat.normand1999,
                    var.names=c("id", "group", "mean", "sd", "n"))
dat.long
#>    study             source n1i m1i sd1i n2i m2i sd2i id group mean sd   n
#> 1      1          Edinburgh 155  55   47 156  75   64  1     1   55 47 155
#> 2      1          Edinburgh 155  55   47 156  75   64  1     2   75 64 156
#> 3      2     Orpington-Mild  31  27    7  32  29    4  2     1   27  7  31
#> 4      2     Orpington-Mild  31  27    7  32  29    4  2     2   29  4  32
#> 5      3 Orpington-Moderate  75  64   17  71 119   29  3     1   64 17  75
#> 6      3 Orpington-Moderate  75  64   17  71 119   29  3     2  119 29  71
#> 7      4   Orpington-Severe  18  66   20  18 137   48  4     1   66 20  18
#> 8      4   Orpington-Severe  18  66   20  18 137   48  4     2  137 48  18
#> 9      5      Montreal-Home   8  14    8  13  18   11  5     1   14  8   8
#> 10     5      Montreal-Home   8  14    8  13  18   11  5     2   18 11  13
#> 11     6  Montreal-Transfer  57  19    7  52  18    4  6     1   19  7  57
#> 12     6  Montreal-Transfer  57  19    7  52  18    4  6     2   18  4  52
#> 13     7          Newcastle  34  52   45  33  41   34  7     1   52 45  34
#> 14     7          Newcastle  34  52   45  33  41   34  7     2   41 34  33
#> 15     8               Umea 110  21   16 183  31   27  8     1   21 16 110
#> 16     8               Umea 110  21   16 183  31   27  8     2   31 27 183
#> 17     9            Uppsala  60  30   27  52  23   20  9     1   30 27  60
#> 18     9            Uppsala  60  30   27  52  23   20  9     2   23 20  52