Skip to content
Prev 31523 / 398506 Next

Creating Dummy Variables with if else phrase

langensk at fas.harvard.edu writes:
is.real is true for all elements in x. You may have intended
!is.na(x[i]).

However, there are simpler ways, e.g.

0+outer(x,1:4,"==")
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    0    1    0    0
[3,]   NA   NA   NA   NA
[4,]    0    0    0    1

or simply use model formulas and code x as a factor.