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.
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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With