factor_ueb: factor_ueb Function

View source: R/factor_ueb.R

factor_uebR Documentation

factor_ueb Function

Description

The function factor is used to encode a vector as a factor (the terms ‘category’ and ‘enumerated type’ are also used for factors). Return original label.

Usage

factor_ueb(
  x,
  levs = NULL,
  labs = NULL,
  del.empty.val = TRUE,
  name.var = NULL,
  write_changes = T
)

Arguments

x

a vector of data, usually taking a small number of distinct values.

levs

a vector of the values (as character strings) that x might have taken. The default is the unique set of values taken by as.character(x), sorted into increasing order of x. Note that this set can be specified as smaller than sort(unique(x))

labs

a character vector of labels for the levels (in the same order as levels after removing those in exclude).

del.empty.val

elimina los niveles que tienen 0 casos

name.var

nom de la variable. Per defecte agafa la part dreta del "$" (exemple: mtcars$mpg, utilitza "mpg")

Value

factor returns an object of class "factor" which has a set of integer codes the length of x with a "levels" attribute of mode character and unique (!anyDuplicated(.)) entries. If argument ordered is true (or ordered() is used) the result has class c("ordered", "factor"). Undocumentedly for a long time, factor(x) loses all attributes(x) but "names", and resets "levels" and "class".

Author(s)

Miriam Mota miriam.mota@vhir.org

Examples

#create variable
variable <- c(0,0,0,1,1,1,0,"a",1,0, "B")
# automatic factor
factor_ueb(x = variable)
# assign levels and label factor
(var_factor <- factor_ueb(variable, levs = c(0,1,2), labs = c("No", "Si","NSNC")))
#reorder factor
factor_ueb(x = var_factor, labs = c("Si","No"))

var_factor2 <- factor(variable)
var_factor2[var_factor2 =="B"] <- NA
factor_ueb(var_factor2, labs = c("1", "a","B","0") )

## Other examples
variable <- c(0,0,0,1,1,1,0,10,1,0)
factor_ueb(variable)
factor_ueb(variable, levs = c(0,1), labs = c("No", "Si"))

variable <- c(0,0,0,1,1,1,0,1,1,0, NA)
factor_ueb(variable)
factor_ueb(variable, levs = c(0,1), labs = c("No", "Si"))

uebvhir/anaStatsUEB documentation built on May 6, 2023, 3:46 a.m.