Learn R Programming

RcppExamples (version 0.1.10)

factor2char: Convert Index and String Vector into CharacterVector, and vice versa

Description

These two functions are an illustration of how as.character and as.factor may be reimplemented at the C++ level.

Usage

factor2char(iv)

char2factor(sv)

Value

A Character Vector which at each position contains the level value of the corresponding index, or a Factor, depending on the function

Arguments

iv

A Integer Vector corresponding to numeric representation of the factor This vector is also expected to have an attribute ‘levels’ with the factor levels

sv

A String Vector

Examples

Run this code
f <- as.factor(c("red", "green", "blue", "red"))
factor2char(f);
f <- as.factor(c("red", "green", "blue", "red"))
v <- factor2char(f);
char2factor(v)

Run the code above in your browser using DataLab