-1

I have a dataframe which looks like this

uid     language_name
1   333        English
2   333        French
3   333        Dutch   
4   654        Spanish 
5   546        English
6   546        French 
7   432        Afrikaans  
8   302        German
9   302        Dutch 

And I want it looking like this:

uid     English              French               Dutch         ..........
1   333        1                   1                    1
2   654        0                   0                    0
3   546        1                   1                    0
4   432        0                   0                    0
5   302        0                   0                    1

So I want to change my language_name column into different dummy columns with the languages as their names. Is there a quick and easy way to do this?

Heroka
  • 12,889
  • 1
  • 28
  • 38

2 Answers2

1

Try with reshape2. Like here:

library("reshape2")

dcast(cbind(z, ile=1), 
      uid~language_name, value.var="ile", fill=0) -> t

colnames(t)[-1] <- paste("Language_", colnames(t)[-1], sep="")
Marta
  • 3,032
  • 3
  • 17
  • 34
0

Usually better way for such dummy coding is to use sparse matrices and Matrix::sparse.model.matrix :

Matrix::sparse.model.matrix(uid ~ -1 + ., data=your_data_frame)
Dmitriy Selivanov
  • 4,545
  • 1
  • 22
  • 38