如何自动为每列创建bin

时间:2017-06-14 05:29:47

标签: r

我有一个数据框如下

date           A    B  ...    Z    
2014-12-01     22   33 ...    44  
2014-12-02     33   44 ...    55  
.  
.  
2014-12-30     65   75 ...    85 

我可以使用以下代码

计算每列的bin
df$A_Bin<-cut(df$A, breaks = c(0, seq(1, 1000, by = 5)), labels = 0:199)    
df$B_Bin<-cut(df$B, breaks = c(0, seq(1, 1000, by = 5)), labels = 0:199) 

如何自动代码

o/p   
date           A    B  ...    Z    A_Bin  B_bin  ... Z_Bin  
2014-12-01     22   33 ...    44   5       7     ... 9  
2014-12-02     33   44 ...    55   7       9     ... 11
.  
.

提前致谢

2 个答案:

答案 0 :(得分:1)

您可以编写一个包含单个向量的函数:

myBins <- function(x) cut(x, breaks = c(0, seq(1, 1000, by = 5)), labels = 0:199)  

然后将其应用于数据框的列,第1列除外:

binned <- sapply(df[,-1], myBins)

为其命名列:

colnames(binned) <- paste0(LETTERS, "_Bin")

并将结果添加到原始df

newdf <- cbind(df, binned) 

答案 1 :(得分:0)

我们可以使用lapply

中的base R
df[paste0(names(df)[-1], "_Bin")] <- lapply(df[-1], cut, 
          breaks = c(0, seq(1, 1000, by = 5), labels = 0:199)