Q: change data frame into another data frame with 1 and 0 I have the data frame dat1 and I want to have the data frame dat2. How can I convert dat1 to the format of dat2 filled with 0 and 1?
s <- c("A","B","C","D","E")
h <- c("H1","H2","H1","H3","H4")
dat1 <- data.frame(s,h)

dat2:
  H1 H2 H3 H4
A 1  0  0  0
B 0  1  0  0
C 1  0  0  0
E 0  0  1  0
F 0  0  0  1


A: Use table to get a 0/1 table and then convert that to a data frame.
as.data.frame.matrix(table(dat1))

giving:
  H1 H2 H3 H4
A  1  0  0  0
B  0  1  0  0
C  1  0  0  0
D  0  0  1  0
E  0  0  0  1

