MrClean
MrClean

Reputation: 1460

Turning a vector into a matrix

I have a vector of 5 values as follows containing different months as follows:

[July August September October November]

How do I get these into a matrix form as follows.

[July August September October November]
[1,    0,       0,       0,       0    ]
[0,    1,       0,       0,       0    ]
[0,    0,       1,       0,       0    ]
[0,    0,       0,       1,       0    ]
[0,    0,       0,       0,       1    ]

Additionally I have another vector I would like to append on the end so then the output would look like this.

[July August September October November Other]
[1,    0,       0,       0,       0,      50 ]
[1,    0,       0,       0,       0,      30 ]
[1,    0,       0,       0,       0,      60 ]
[1,    0,       0,       0,       0,      22 ]
[1,    0,       0,       0,       0,      5  ]

Upvotes: 1

Views: 48

Answers (3)

d.b
d.b

Reputation: 32558

sapply(v1, function(x) as.numeric(v1 == x))
#     July August September October November
#[1,]    1      0         0       0        0
#[2,]    0      1         0       0        0
#[3,]    0      0         1       0        0
#[4,]    0      0         0       1        0
#[5,]    0      0         0       0        1

Upvotes: 1

akrun
akrun

Reputation: 887821

We can use table with the sequence of vector and the vector itself. If the order of month names needs to be maintained, convert it to a factor with levels specified by the intersect of unique elements of vector and the month.name

tbl <- table(seq_along(v1), v2, dnn = NULL)
tbl
#   July August September October November
#1    1      0         0       0        0
#2    0      1         0       0        0
#3    0      0         1       0        0
#4    0      0         0       1        0
#5    0      0         0       0        1

Now, we can append with second vector

cbind(tbl, Other)

Or using model.matrix

model.matrix(~ v2 - 1, data = data.frame(v2))

Or using mtabulate from qdapTools

library(qdapTools)
mtabulate(v2)

data

v1 <- c("July", "August", "September", "October", "November")
v2 <- factor(v1, levels = intersect(month.name, unique(v1)))

Upvotes: 1

Sonny
Sonny

Reputation: 3183

You can use model.matrix or dummyVars also

Data:

v1 <- c("July", "August", "September", "October", "November")
v1 <- as.data.frame(v1)

Model.Matrix:

v2 <- data.frame(v1 = v1)
as.data.frame(model.matrix(~.-1, v1))
  v1August v1July v1November v1October v1September
1        0      1          0         0           0
2        1      0          0         0           0
3        0      0          0         0           1
4        0      0          0         1           0
5        0      0          1         0           0

DummyVars approach:

library(caret)
m1 <- dummyVars(" ~ .", data = v1)
data.frame(predict(m1, newdata = v1))
  v1.August v1.July v1.November v1.October v1.September
1         0       1           0          0            0
2         1       0           0          0            0
3         0       0           0          0            1
4         0       0           0          1            0
5         0       0           1          0            0

Upvotes: 0

Related Questions