Add Marginal Observations
addMarginal.RdGiven a data frame and the names of variable, doubles the
data frame for each variable with a new category
"All" by default, or by the value of label.
A new variable .marginal. is added to the resulting data frame,
with value "" if the observation is an original one, and with
value equal to the names of the variable being marginalized (separated
by commas) otherwise. If there is another stratification variable
besides the one in ..., and that variable is nested inside the
variable in ..., specify nested=variable name to have the value
of that variable set fo label whenever marginal observations are
created for .... See the state-city example below.
Usage
addMarginal(data, ..., label = "All", margloc=c('last', 'first'), nested)Arguments
- data
a data frame
- ...
a list of names of variables to marginalize
- label
category name for added marginal observations
- margloc
location for marginal category within factor variable specifying categories. Set to
"first"to override the default - to put a category with valuelabelas the first category.- nested
a single unquoted variable name if used
Examples
d <- expand.grid(sex=c('female', 'male'), country=c('US', 'Romania'),
reps=1:2)
addMarginal(d, sex, country)
#> sex country reps .marginal.
#> 1 female US 1
#> 2 male US 1
#> 3 female Romania 1
#> 4 male Romania 1
#> 5 female US 2
#> 6 male US 2
#> 7 female Romania 2
#> 8 male Romania 2
#> 9 All US 1 sex
#> 10 All US 1 sex
#> 11 All Romania 1 sex
#> 12 All Romania 1 sex
#> 13 All US 2 sex
#> 14 All US 2 sex
#> 15 All Romania 2 sex
#> 16 All Romania 2 sex
#> 17 female All 1 country
#> 18 male All 1 country
#> 19 female All 1 country
#> 20 male All 1 country
#> 21 female All 2 country
#> 22 male All 2 country
#> 23 female All 2 country
#> 24 male All 2 country
#> 25 All All 1 sex,country
#> 26 All All 1 sex,country
#> 27 All All 1 sex,country
#> 28 All All 1 sex,country
#> 29 All All 2 sex,country
#> 30 All All 2 sex,country
#> 31 All All 2 sex,country
#> 32 All All 2 sex,country
# Example of nested variables
d <- data.frame(state=c('AL', 'AL', 'GA', 'GA', 'GA'),
city=c('Mobile', 'Montgomery', 'Valdosto',
'Augusta', 'Atlanta'),
x=1:5, stringsAsFactors=TRUE)
addMarginal(d, state, nested=city) # cite set to 'All' when state is
#> state city x .marginal.
#> 1 AL Mobile 1
#> 2 AL Montgomery 2
#> 3 GA Valdosto 3
#> 4 GA Augusta 4
#> 5 GA Atlanta 5
#> 6 All All 1 state
#> 7 All All 2 state
#> 8 All All 3 state
#> 9 All All 4 state
#> 10 All All 5 state