从分类变量创建新的虚拟变量列

Dre*_*way 39 r

我有几个数据集,75,000个观测值和一个type可以取值0-4 的变量.我想为所有类型的每个数据集添加五个新的虚拟变量.我能想出的最佳方法如下:

# For the 'binom' data set create dummy variables for all types in all data sets
binom.dummy.list<-list()
for(i in 0:4){
    binom.dummy.list[[i+1]]<-sapply(binom$type,function(t) ifelse(t==i,1,0))
}

# Add and merge data
binom.dummy.df<-as.data.frame(do.call("cbind",binom.dummy.list))
binom.dummy.df<-transform(binom.dummy.df,id=1:nrow(binom))
binom<-merge(binom,binom.dummy.df,by="id")
Run Code Online (Sandbox Code Playgroud)

虽然这很有效,但速度非常慢(合并功能甚至已经崩溃了几次).有没有更有效的方法来做到这一点?也许这个功能是我不熟悉的软件包的一部分?

gap*_*ppy 49

R有一个"子语言"将公式转换为设计矩阵,并且在语言的精神上你可以利用它.它快速而简洁.示例:您有一个基数预测器x,一个分类预测器catVar和一个响应y.

> binom <- data.frame(y=runif(1e5), x=runif(1e5), catVar=as.factor(sample(0:4,1e5,TRUE)))
> head(binom)
          y          x catVar
1 0.5051653 0.34888390      2
2 0.4868774 0.85005067      2
3 0.3324482 0.58467798      2
4 0.2966733 0.05510749      3
5 0.5695851 0.96237936      1
6 0.8358417 0.06367418      2
Run Code Online (Sandbox Code Playgroud)

你这样做

> A <- model.matrix(y ~ x + catVar,binom) 
> head(A)
  (Intercept)          x catVar1 catVar2 catVar3 catVar4
1           1 0.34888390       0       1       0       0
2           1 0.85005067       0       1       0       0
3           1 0.58467798       0       1       0       0
4           1 0.05510749       0       0       1       0
5           1 0.96237936       1       0       0       0
6           1 0.06367418       0       1       0       0
Run Code Online (Sandbox Code Playgroud)

完成.

  • 任何相反方向的简单方法 - 即你有虚拟变量,但想将它们折叠成一个变量? (6认同)

Jos*_*ich 24

德鲁,这要快得多,不应该造成任何崩溃.

> binom <- data.frame(data=runif(1e5),type=sample(0:4,1e5,TRUE))
> for(t in unique(binom$type)) {
+   binom[paste("type",t,sep="")] <- ifelse(binom$type==t,1,0)
+ }
> head(binom)
        data type type2 type4 type1 type3 type0
1 0.11787309    2     1     0     0     0     0
2 0.11884046    4     0     1     0     0     0
3 0.92234950    4     0     1     0     0     0
4 0.44759259    1     0     0     1     0     0
5 0.01669651    2     1     0     0     0     0
6 0.33966184    3     0     0     0     1     0
Run Code Online (Sandbox Code Playgroud)

  • 好的解决方案 我可以建议,在"粘贴"之前加入一点"make.names",以防等级名称包含一些诉讼角色. (2认同)

gri*_*orz 16

那么使用model.matrix()呢?

> binom <- data.frame(data=runif(1e5),type=sample(0:4,1e5,TRUE))
> head(binom)
       data type
1 0.1412164    2
2 0.8764588    2
3 0.5559061    4
4 0.3890109    3
5 0.8725753    3
6 0.8358100    1
> inds <- model.matrix(~ factor(binom$type) - 1)
> head(inds)
  factor(binom$type)0 factor(binom$type)1 factor(binom$type)2 factor(binom$type)3 factor(binom$type)4
1                   0                   0                   1                   0                   0
2                   0                   0                   1                   0                   0
3                   0                   0                   0                   0                   1
4                   0                   0                   0                   1                   0
5                   0                   0                   0                   1                   0
6                   0                   1                   0                   0                   0
Run Code Online (Sandbox Code Playgroud)