将分类列转换为多个二进制列 [英] Convert categorical column to multiple binary columns

查看:26
本文介绍了将分类列转换为多个二进制列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to convert this column into binary columns for each breed (1 dog is breed, 0 dog is not that breed)

推荐答案

一种方法是使用 uniquefor-loop

One way could be using unique with a 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)
}

这篇关于将分类列转换为多个二进制列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆