自动将 R 因子扩展为每个因子水平的 1/0 指标变量的集合 [英] Automatically expanding an R factor into a collection of 1/0 indicator variables for every factor level

查看:52
本文介绍了自动将 R 因子扩展为每个因子水平的 1/0 指标变量的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 R 数据框,其中包含一个我想要扩展"的因子,因此对于每个因子级别,新数据框中都有一个关联列,其中包含一个 1/0 指标.例如,假设我有:

I have an R data frame containing a factor that I want to "expand" so that for each factor level, there is an associated column in a new data frame, which contains a 1/0 indicator. E.g., suppose I have:

df.original <-data.frame(eggs = c("foo", "foo", "bar", "bar"), ham = c(1,2,3,4))

我想要:

df.desired  <- data.frame(foo = c(1,1,0,0), bar=c(0,0,1,1), ham=c(1,2,3,4))

因为对于某些需要完全数字化的数据框的分析(例如主成分分析),我认为可能会内置此功能.编写一个函数来执行此操作应该不会太难,但我可以预见与列名有关的一些挑战,如果已经存在某些内容,我宁愿使用它.

Because for certain analyses for which you need to have a completely numeric data frame (e.g., principal component analysis), I thought this feature might be built in. Writing a function to do this shouldn't be too hard, but I can foresee some challenges relating to column names and if something exists already, I'd rather use that.

推荐答案

使用model.matrix函数:

model.matrix( ~ Species - 1, data=iris )

这篇关于自动将 R 因子扩展为每个因子水平的 1/0 指标变量的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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