将分类列转换为多个二进制列

时间:2017-06-25 15:44:41

标签: r tidyr

我想将此列转换为每个品种的二进制列(1只狗是品种,0只狗不是那个品种)

enter image description here

2 个答案:

答案 0 :(得分:2)

使用model.matrix()转换二进制变量中的分类变量。

Breed = c(
  "Sheetland Sheepdog Mix",
  "Pit Bull Mix",
  "Lhasa Aposo/Miniature",
  "Cairn Terrier/Chihuahua Mix",
  "American Pitbull",
  "Cairn Terrier",
  "Pit Bull Mix"
)
df=data.frame(Breed)

dfcat = data.frame(model.matrix(~ df$Breed-1, data=df))
names(dfcat) = levels(df$Breed)

所以dfcat包含你的二进制变量:

dfcat
#American Pitbull Cairn Terrier Cairn Terrier/Chihuahua Mix Lhasa Aposo/Miniature Pit Bull Mix Sheetland Sheepdog Mix
#              0             0                           0                     0            0                      1
#              0             0                           0                     0            1                      0
#              0             0                           0                     1            0                      0
#              0             0                           1                     0            0                      0
#              1             0                           0                     0            0                      0
#              0             1                           0                     0            0                      0
#              0             0                           0                     0            1                      0

答案 1 :(得分:0)

一种方法是将uniquefor-loop

一起使用
Breed = c(
  "Sheetland Sheepdog Mix",
  "Pit Bull Mix",
  "Lhasa Aposo/Miniature",
  "Cairn Terrier/Chihuahua Mix",
  "American Pitbull",
  "Cairn Terrier",
  "Pit Bull Mix"
)
df=data.frame(Breed)

for (i in unique(df$breed)){
  df[,paste0(i)]=ifelse(df$Breed==i,1,0)
}