seqrecode: Recoding state sequence objects and factors

View source: R/seqrecode.R

seqrecodeR Documentation

Recoding state sequence objects and factors


Utilities for recoding factors or state sequence objects created with seqdef.


seqrecode(seqdata, recodes, otherwise = NULL,
     labels = NULL, cpal = NULL)
recodef(x, recodes, otherwise=NULL, na=NULL)



The state sequence object to be recoded (created with seqdef).


A list specifying the recoding operations where each element is in the form newcode=oldcode or newcode=c(oldcode1, oldcode2,...). The rules are treated in the same order as they appear, hence subsequent rules may modify the first ones.


NULL or Character. Level given to cases uncovered by the recodes list. If NULL, old states remain unchanged.


optional state labels used for the color legend of TraMineR's graphics. If NULL (default), the state names in the alphabet are also used as state labels (see seqdef).


an optional color palette for representing the newly defined alphabet in graphics. If NULL (default), a color palette is created from the colors in seqdata by assigning to newcode the color of the first old state listed as oldcode and by leaving the colors of the other states unchanged.


A factor to be recoded.


Character vector. If not NULL, the list of states that should be recoded as NA (missing values).


The recoded factor or state sequence object.


Matthias Studer (with Gilbert Ritschard for the help page)

See Also

seqdef to create a state sequence object.


## Recoding a state sequence object with seqrecode
## Creating a state sequence object
actcal.seq <- seqdef(actcal,13:24, labels=c("> 37 hours", "19-36 hours",
   "1-18 hours", "no work"))
## Regrouping states B and C and setting the whole alphabet to A BC D <-seqrecode(actcal.seq,
    recodes = list("A"="A", "BC"=c("B", "C"), "D"="D"))
## Crosstabulate the first column of the recoded and
## original state sequence objects
table([,1], actcal.seq[,1])

## Same as before but using automatically original
## codes for unspecified states.
actcal.new2 <-seqrecode(actcal.seq,
	recodes = list("BC"=c("B", "C")))
table(actcal.new2[,1], actcal.seq[,1])

## Same as before but using otherwise
actcal.new3 <-seqrecode(actcal.seq, recodes = list("A"="A", "D"="D"),
table(actcal.new3[,1], actcal.seq[,1])

## Recoding factors
## Recoding the marital status to oppose married to all other case
maritalstatus <- recodef(actcal$civsta00,
	recodes=list("Married"="married"), otherwise="Single")
table(maritalstatus, actcal$civsta00)

## Recoding the number of kids in the household
## -2 is a missing value
nbkids <- recodef(actcal$nbkid00,
   recodes=list("None"=0, "One"=1, "Two or more"=2:10), na=-2)
table(nbkids, actcal$nbkid00, useNA="always")

TraMineR documentation built on May 29, 2024, 5 a.m.