O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

The dummy.data.frame() function creates dummies for all the factors in the data frame supplied. Internally, it uses another dummy() function which creates dummy variables for a single factor. The dummy() function creates one new variable for every level of the factor for which we are creating dummies. It appends the variable name with the factor level name to generate names for the dummy variables. We can use the sep argument to specify the character that separates them; an empty string is the default:

> dummy(students$State, sep = ".")        State.NJ State.NY State.TX State.VA  [1,]        1        0        0        0  [2,]        0        1        0        0  [3,]        1        0        0        0  [4,]        0        0        0        1  [5,]        0        1        0        0  [6,]        0        0        1        0  [7,]        1        0        0        0  [8,]        0        0        0        1  [9,]        0        0        1        0 [10,]        0        0        0        1 

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required