labels - Create a factor with more levels as actual used by default in R -


i wrote function in r tabulate patient characteristics. if have tabulate nominal variable works fine, if there no nas different categories.

for example:

i tabulate nyha class @ baseline studyarm. nyha class has categories "no", "nyha i", "nyha ii", "nyha iii", "nyha iv", , maybe "nyha unknown".

in data nyha class known (the category "nyha unknown" missing). in patient characteristic table (pct) want line category "nyha unknown" well.

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, have code labels index (here [1:5]). category "nyha unknown" missing.

it can added afterwards:

levels(testvarf)<-testvarlab 

this solution not useful because of hard indexed labels. use pct check data during recruiting. here normal, codes/labels missing in beginning.

so question simple:

how can define factor possible labels when not labels used?

thank help!

volker

use levels argument factor (see ?factor), providing possible levels.

factor(testvar, levels=testvarlab) 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -