答案 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)
一种方法是将unique
与for-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)
}