Melt a Dataset To Examine All Xs vs Y

meltData(
  formula,
  data,
  tall = c("right", "left"),
  vnames = c("labels", "names"),
  sepunits = FALSE,
  ...
)

Arguments

formula

a formula

data

data frame or table

tall

see above

vnames

set to names to always use variable names instead of labels for X

sepunits

set to TRUE to create a separate variable Units to hold units of measurement. The variable is not created if no original variables have a non-blank units attribute.

...

passed to label()

Value

data table

Details

Uses a formula with one or more left hand side variables (Y) and one or more right hand side variables (X). Uses data.table::melt() to melt data so that each X is played against the same Y if tall='right' (the default) or each Y is played against the same X combination if tall='left'. The resulting data table has variables Y with their original names (if tall='right') or variables X with their original names (if tall='left'), variable, and value. By default variable is taken as label()s of the tall variables.

See also

Author

Frank Harrell

Examples

d <- data.frame(y1=(1:10)/10, y2=(1:10)/100, x1=1:10, x2=101:110)
label(d$x1) <- 'X1'
units(d$x1) <- 'mmHg'
m=meltData(y1 + y2 ~ x1 + x2, data=d, units=TRUE) # consider also html=TRUE
print(m)
#>        y1    y2   variable value
#>     <num> <num>     <fctr> <num>
#>  1:   0.1  0.01 X1  [mmHg]     1
#>  2:   0.2  0.02 X1  [mmHg]     2
#>  3:   0.3  0.03 X1  [mmHg]     3
#>  4:   0.4  0.04 X1  [mmHg]     4
#>  5:   0.5  0.05 X1  [mmHg]     5
#>  6:   0.6  0.06 X1  [mmHg]     6
#>  7:   0.7  0.07 X1  [mmHg]     7
#>  8:   0.8  0.08 X1  [mmHg]     8
#>  9:   0.9  0.09 X1  [mmHg]     9
#> 10:   1.0  0.10 X1  [mmHg]    10
#> 11:   0.1  0.01         x2   101
#> 12:   0.2  0.02         x2   102
#> 13:   0.3  0.03         x2   103
#> 14:   0.4  0.04         x2   104
#> 15:   0.5  0.05         x2   105
#> 16:   0.6  0.06         x2   106
#> 17:   0.7  0.07         x2   107
#> 18:   0.8  0.08         x2   108
#> 19:   0.9  0.09         x2   109
#> 20:   1.0  0.10         x2   110
#>        y1    y2   variable value
m=meltData(y1 + y2 ~ x1 + x2, data=d, tall='left')
print(m)
#>             x1    x2 variable value
#>     <labelled> <int>   <fctr> <num>
#>  1:          1   101       y1  0.10
#>  2:          2   102       y1  0.20
#>  3:          3   103       y1  0.30
#>  4:          4   104       y1  0.40
#>  5:          5   105       y1  0.50
#>  6:          6   106       y1  0.60
#>  7:          7   107       y1  0.70
#>  8:          8   108       y1  0.80
#>  9:          9   109       y1  0.90
#> 10:         10   110       y1  1.00
#> 11:          1   101       y2  0.01
#> 12:          2   102       y2  0.02
#> 13:          3   103       y2  0.03
#> 14:          4   104       y2  0.04
#> 15:          5   105       y2  0.05
#> 16:          6   106       y2  0.06
#> 17:          7   107       y2  0.07
#> 18:          8   108       y2  0.08
#> 19:          9   109       y2  0.09
#> 20:         10   110       y2  0.10
#>             x1    x2 variable value