Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: reshaping dataframe into matrix of 1's and 0's

Tags:

r

I'm trying to convert dataframe of this type format:

  V1 V2
  1  a
  2  a
  3  b
  4  c
  5  c

into a matrix of this format:

  V1 a  b  c
  1  1  0  0
  2  1  0  0
  3  0  1  0
  4  0  0  1
  5  0  0  1

What is the best way to do this in R? I've tried to use reshape2, but couldn't figure out a way to do this.

like image 528
user4130588 Avatar asked Jul 02 '15 14:07

user4130588


3 Answers

table should be sufficient for this:

with(mydf, cbind(V1, table(1:nrow(mydf), V2)))
##   V1 a b c
## 1  1 1 0 0
## 2  2 1 0 0
## 3  3 0 1 0
## 4  4 0 0 1
## 5  5 0 0 1

Alternatively, you can look at model.matrix:

cbind(mydf["V1"], model.matrix(~V2 + 0, mydf))
##   V1 V2a V2b V2c
## 1  1   1   0   0
## 2  2   1   0   0
## 3  3   0   1   0
## 4  4   0   0   1
## 5  5   0   0   1
like image 65
A5C1D2H2I1M1N2O1R2T1 Avatar answered Nov 03 '22 15:11

A5C1D2H2I1M1N2O1R2T1


Maybe is a shortcut but that's not the same of this?

library(reshape2)
dcast(dat, V1 ~ V2, length )
Using V2 as value column: use value.var to override.
  V1 a b c
1  1 1 0 0
2  2 1 0 0
3  3 0 1 0
4  4 0 0 1
5  5 0 0 1
like image 34
SabDeM Avatar answered Nov 03 '22 16:11

SabDeM


I'm not familiar with the special functions for this, but I might do...

uv <- unique(DF$V2)
m  <- matrix(0L,nrow(DF),length(uv),dimnames=list(DF$V1,uv))
m[ cbind(1:nrow(m), match(DF$V2,uv)) ] <- 1L

This is a matrix of zeros and ones, unlike the other answers so far. (Of course, small difference.)

  a b c
1 1 0 0
2 1 0 0
3 0 1 0
4 0 0 1
5 0 0 1
like image 2
Frank Avatar answered Nov 03 '22 15:11

Frank