ms_flatten | R Documentation |
Convert a multiple-response object into a named numeric vector using a supplied ordering.
ms_flatten(x, priorities, fun, start=0)
x |
|
priorities |
Character vector of levels. |
fun |
Function for reducing two values to one. |
start |
starting value for |
Each observation is initially assigned the value start
. Starting with the lowest-priority level, the current value is combined with the new value as fun(new, current)
. Using fun=function(x,y) x
would return the value for the highest-priority level present; using fun=pmax
would return the highest score for any level present; using fun="+"
would return the sum of the scores.
A factor
data(ethnicity)
ethnicity
## NZ 'prioritised ethnicity'
eth <- mr_recode(ethnicity, `European/Other`="European", `European/Other` = "MELAA")
mr_flatten(ethnicity, c("Maori","Pacific","Asian","European/Other"))
data(nzbirds)
## hardest to see first
ms_flatten(nzbirds, c("kaki","ruru","kea","tui","tauhou"),"+")
ms_flatten(nzbirds, c("kaki","ruru","kea","tui","tauhou"),
fun=function(x,y) x)
ms_flatten(nzbirds, c("kaki","ruru","kea","tui","tauhou"),pmin,start=Inf)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.