Score a Series of Binary Variables
score.binary.RdCreates a new variable from a series of logical conditions. The new
variable can be a hierarchical category or score derived from considering
the rightmost TRUE value among the input variables, an additive point
score, a union, or any of several others by specifying a function using the
fun argument.
Arguments
- ...
a list of variables or expressions which are considered to be binary or logical
- fun
a function to compute on each row of the matrix represented by a specific observation of all the variables in
...- points
points to assign to successive elements of
.... The default is1, 2, ..., p, wherepis the number of elements. If you specify one number forpoints, that number will be duplicated (i.e., equal weights are assumed).- na.rm
set to
TRUEto removeNAs from consideration when processing each row of the matrix of variables in.... Forfun=max,na.rm=TRUEis the default sincescore.binaryassumes that a hierarchical scale is based on available information. Otherwise,na.rm=FALSEis assumed. Forfun=meanyou may want to specifyna.rm=TRUE.- retfactor
applies if
fun=max, in which caseretfactor=TRUEmakesscore.binaryreturn afactorobject since a hierarchical scale implies a unique choice.
Value
a factor object if retfactor=TRUE and fun=max or a numeric vector
otherwise. Will not contain NAs if na.rm=TRUE unless every variable in
a row is NA. If a factor object
is returned, it has levels "none" followed by character
string versions of the arguments given in ... .
Examples
set.seed(1)
age <- rnorm(25, 70, 15)
previous.disease <- sample(0:1, 25, TRUE)
#Hierarchical scale, highest of 1:age>70 2:previous.disease
score.binary(age>70, previous.disease, retfactor=FALSE)
#> [1] 2 1 2 2 1 0 2 2 2 0 1 2 2 2 2 2 2 1 2 2 2 2 1 0 1
#Same as above but return factor variable with levels "none" "age>70"
# "previous.disease"
score.binary(age>70, previous.disease)
#> [1] previous.disease age > 70 previous.disease previous.disease
#> [5] age > 70 none previous.disease previous.disease
#> [9] previous.disease none age > 70 previous.disease
#> [13] previous.disease previous.disease previous.disease previous.disease
#> [17] previous.disease age > 70 previous.disease previous.disease
#> [21] previous.disease previous.disease age > 70 none
#> [25] age > 70
#> Levels: none age > 70 previous.disease
#Additive scale with weights 1:age>70 2:previous.disease
score.binary(age>70, previous.disease, fun=sum)
#> [1] 2 1 2 3 1 0 3 3 3 0 1 3 2 2 3 2 2 1 3 3 3 3 1 0 1
#Additive scale, equal weights
score.binary(age>70, previous.disease, fun=sum, points=c(1,1))
#> [1] 1 1 1 2 1 0 2 2 2 0 1 2 1 1 2 1 1 1 2 2 2 2 1 0 1
#Same as saying points=1
#Union of variables, to create a new binary variable
score.binary(age>70, previous.disease, fun=any)
#> [1] TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE FALSE TRUE TRUE
#> [13] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE
#> [25] TRUE