我有数据帧dat1,我想有数据帧dat2。如何将dat1转换为0和1填充的dat2格式?
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
答案 0 :(得分:0)
使用table
获取一个0/1表,然后将其转换为数据帧。
as.data.frame.matrix(table(dat1))
给予:
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