Create factor with more levels like the actual default in R

I wrote a function in R to tabulate patient characteristics. If I need to specify a nominal variable, it works fine as long as there is no NA for different categories.

For example:

I am putting together a NYHA spreadsheet at a basic level in Studyarm. The NYHA class usually has the categories "None", "NYHA I", "NYHA II", "NYHA III", "NYHA IV" and possibly "NYHA unknown".

In my data, the NYHA class is always known (the "NYHA unknown" category is missing). However, on my Patient Characteristics Table (PCT), I need a line with the category "NYHA unknown".

This code:

testvarlab = c("no HI","NYHA I","NYHA II","NYHA III","NYHA IV","NYHA unknown")
testvarf<-factor(testvar,labels=testvarlab[1:5]);class(testvarf);table(testvarf)

      

works fine, but I have to encode the labels using INDEX (here [1: 5]). There is no "NYHA unknown" category.

It can be added afterwards:

levels(testvarf)<-testvarlab

      

This solution is not useful due to hard indexed tags. I use this PCT for background checks while recruiting. It is normal here that some codes / labels are missing at the beginning.

So my question is simple:

How can I determine a coefficient with all possible labels, even if not all labels are used on it?

Thanks for the help!

Volker

+3


source to share


1 answer


Use the argument levels

for factor

(see ?factor

), providing all possible levels.



factor(testvar, levels=testvarlab)

      

+1


source







All Articles