Transform one column from categoric to binary, keep the rest

  • A+
Category:Languages

I have a medium large dataframe, for which I want to transform one column with categories to binary columns, one for each category.

At the same time, I want to keep the rest of the columns in the dataframe.

What would be the easiest way to achieve this?

Here is an example of what I want to do:

d<-data.frame(ID=c("a","b","c","d"), Gender=c("male", "male", "female","female"), Age =c(23,45,18,11))   ID Gender Age 1  a   male  23 2  b   male  45 3  c female  18 4  d female  11 

should look as d2 afterwards, so that the ID and Age columns are still there and untouched:

d2<-data.frame(ID=c("a","b","c","d"), Gender.male=c(1, 1, 0, 0), Gender.female=c(0,0,1,1), Age =c(23,45,18,11))    ID Gender.male Gender.female Age 1  a           1             0  23 2  b           1             0  45 3  c           0             1  18 4  d           0             1  11 

 


We can use spread

library(tidyvesre) d %>%    mutate(n = 1) %>%    spread(Gender, n, fill = 0) 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: