【问题标题】:Efficiently combine multiple binary/categorical columns to single categorical column in R有效地将多个二进制/分类列组合到 R 中的单个分类列
【发布时间】:2018-11-30 00:27:18
【问题描述】:

首先,我知道以下页面上的相关问题/答案。

Convert multiple binary columns to single categorical column

For each row return the column name of the largest value

但是我的问题略有不同,上面的这些解决方案对我不起作用。

给定具有可能重叠的二进制变量的数据集,将它们组合成单个分类列的最有效方法是什么?

作为一个简单的例子,考虑以下数据集

set.seed(12345)
d1<-data.frame(score=rnorm(10),
               Male=sample(c(rep(1,5), rep(0,5))), 
               White=sample(c(rep(1,5),rep(0,5))), 
               college_ed = rep(c(1,0),5))

head(d1,5)

      score   Male   White college_ed
1  0.5855288    1     0          1
2  0.7094660    1     1          0
3 -0.1093033    0     1          1
4 -0.4534972    0     1          0
5  0.6058875    1     1          1

这里的目标是创建一个新列,该列将列出所有等于 1 的列的名称。

到目前为止,这是我提出的最佳解决方案,但我担心它有点粗糙,如果应用于更大的数据集可能效率不高。

 grp_name<-function(x){
   if(sum(x)==0){
   z<- "None"
   }else{
   z<-paste(names(x[x==1]),collapse= "-")
   }
   return(z)
   }


d1$demo<-apply(d1,1,grp_name)

     score    Male   White    college_ed        demo
1  0.5855288    1     0          1       Male-college_ed
2  0.7094660    1     1          0            Male-White
3 -0.1093033    0     1          1      White-college_ed
4 -0.4534972    0     1          0                 White
5  0.6058875    1     1          1 Male-White-college_ed

任何人都知道一些解决这个问题的包或对加速代码有任何建议吗?

【问题讨论】:

    标签: r data-manipulation binary-data categorical-data


    【解决方案1】:

    这不是一个完美的解决方案,但应该能让您更快地实现目标。 if 语句不会向量化,但 ifelse() 会:见下文....无需使用 apply 函数。

    set.seed(12345)
    d1<-data.frame(score=rnorm(10),
                   Male=sample(c(rep(1,5), rep(0,5))), 
                   White=sample(c(rep(1,5),rep(0,5))), 
                   college_ed = rep(c(1,0),5))
    
    head(d1,5)
    
    makeKey <- function(x,y,z){
      s1 <- ifelse(x == 1,"Male", "")
      s2 <- ifelse(y == 1, "White", "")
      s3 <- ifelse(z == 1, "college_ed", "")
      s4 <- paste(s1,s2,s3, sep = "-" )
      return(s4)
    }
    
    d1$key <- makeKey(x=d1$Male, y=d1$White, z=d1$college_ed)
    

    【讨论】:

    • 好建议!我意识到如果我让示例数据集更大,比较处理时间可能会更好。当我将大小增加到 100 万行时,您的解决方案速度提高了大约 6 倍。
    猜你喜欢
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 2018-04-07
    • 2017-08-29
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多