Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access the levels of the factors of a data.frame in Rcpp when it is passed as an argument from R?

Tags:

r

rcpp

I have the following data.frame df1 with factor fac1 that I have created in R and I pass it as an argument in a Rcpp function. The requested task is to get all the factor levels inside an Rcpp function and use them.

fac1 <- factor(x = sample(x = 1:5,size = 100,replace = T),labels =  paste0("D",1:5))
var1 <- sample(x = 1:20,size = 100,replace = T)
fac2 <- factor(x = sample(x = 1:12,size = 100,replace = T),labels =  paste0("M",1:12))
df1 <- data.frame(fac1,var1,fac2)
like image 552
VassilisGeorgiou Avatar asked Jan 02 '26 01:01

VassilisGeorgiou


1 Answers

After some research I found a solution and I am sharing it.
The Rf_isFactor checks if a data.frame column is a factor or not.
The levels attribute of corresponding IntegerVector (tempVec) returns the factor levels.

cppFunction("void GetFactorLevels(DataFrame df1){
    CharacterVector varNames = df1.names();
    for(int i=0; i<df1.length();i++) {
        if(Rf_isFactor(df1[i])==1){
           IntegerVector tempVec=df1[i];
           CharacterVector factorLevels =tempVec.attr(\"levels\");
           Rcout<<varNames[i]<<\": \"<<factorLevels<<std::endl;
        }
   }
}")

> GetFactorLevels(df1)
fac1: "D1" "D2" "D3" "D4" "D5"
fac2: "M1" "M2" "M3" "M4" "M5" "M6" "M7" "M8" "M9" "M10" "M11" "M12"
like image 186
VassilisGeorgiou Avatar answered Jan 03 '26 20:01

VassilisGeorgiou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!