-1

At the moment I have a data set of a voting data where each person voted on a number of policies either yes or no or they were absent at the time of the vote of that particular policy.

Overall I have 23 policies but I have no idea how to convert the data into binary. The data set is set up in the way that obviously "n" = no , "y" = yes and "a" = absent

If anyone could lend me a hand here as to how to convert the data in R to a binary Matrix I would appreciate it !

2 Answers2

1

This may be done using model.matrix. Note, this is done automatically for you in many cases in R, e.g. regression analysis.

> set.seed(1)
> (df <- data.frame(id=1:10,vote=sample(c("yes","no","absent"),10,replace=TRUE)))
   id   vote
1   1    yes
2   2     no
3   3     no
4   4 absent
5   5    yes
6   6 absent
7   7 absent
8   8     no
9   9     no
10 10    yes
> model.matrix(~.-1,df)
   id voteabsent voteno voteyes
1   1          0      0       1
2   2          0      1       0
3   3          0      1       0
4   4          1      0       0
5   5          0      0       1
6   6          1      0       0
7   7          1      0       0
8   8          0      1       0
9   9          0      1       0
10 10          0      0       1
A. Webb
  • 26,227
  • 1
  • 63
  • 95
0

For example:

m <- as.matrix(cbind(c('y','y','y'),c('n','n','n'),c('a','a','a')))
m[m == 'y'] <- 1
m[m == 'n'] <- 0
m[m == 'a'] <- NA
Ansjovis86
  • 1,506
  • 5
  • 17
  • 48
  • 2
    You should use `TRUE` and `FALSE` instead. And of course, the matrix will still be a character matrix with your approach. – Roland Feb 08 '17 at 15:52
  • Following your approach I might do: `m[m == 'a'] <- NA; m[] <- m == 'y'; storage.mode(m) <- "logical"` – Roland Feb 08 '17 at 16:02