2015-05-27 84 views
1

有關示例數據框:添加標籤削減功能中的R

df1 <- structure(list(X = 1:15, a = c(2L, 3L, 4L, 3L, 7L, 5L, NA, 2L, 
9L, 7L, 0L, 1L, 20L, 15L, 14L)), .Names = c("X", "a"), 
    class = "data.frame", row.names = c(NA, 
-15L)) 

我使用下面的代碼來劃分列「A」到四分:

cut.at.n.tile <- function(X , n = 4){ 
    cut(X , breaks = quantile(X , 
    probs = (0:n)/n , na.rm = TRUE) , include.lowest = TRUE)} 
df1$a.quartile <- cut.at.n.tile(df1$a , n = 4) 

如何更換標籤與1 - 4(1是最低)?我不想簡單地重新編碼這些值,因爲我將用不同的連續變量來運行這麼多次。

任何幫助將不勝感激。

回答

2

使用labels參數cut ...

cut.at.n.tile <- function(X , n = 4){ 
    cut(X , breaks = quantile(X , 
     probs = (0:n)/n , na.rm = TRUE) , 
     labels = 1:n, 
     include.lowest = TRUE)} 
cut.at.n.tile(df1$a , n = 4) 
## [1] 1 2 2 2 3 3 <NA> 1 4 3 
##  1 1 4 4 4 
## Levels: 1 2 3 4 

您可能也有興趣ggplot2::cut_number,這確實 本質上是一回事...

ggplot2::cut_number(df1$a, n = 4, labels = 1:4)