Quantiles by factor levels in R

前端 未结 2 1153
梦毁少年i
梦毁少年i 2020-12-18 04:07

I have a data frame and I\'m trying to create a new variable in the data frame that has the quantiles of a continuous variable var1, for each level of a factor

相关标签:
2条回答
  • 2020-12-18 04:28

    Use ave on your dat data frame. Full example with your simulated data and qfun function:

    # some data
    set.seed(472)
    dat <- data.frame(var1 = rnorm(50, 10, 3)^2,
                  strata = factor(sample(LETTERS[1:5], size = 50, replace = TRUE))
                  )
    
    # function to get quantiles
    qfun <- function(x, q = 5) {
        quantile <- cut(x, breaks = quantile(x, probs = 0:q/q), 
            include.lowest = TRUE, labels = 1:q)
        quantile
    }
    

    And my addition...

    dat$q <- ave(dat$var1,dat$strata,FUN=qfun)
    
    0 讨论(0)
  • 2020-12-18 04:49

    I think your issue is that you don't really want to aggregate, but use ave, (or data.table or plyr)

    qdat <- transform(dat, qq = ave(var1, strata, FUN = qfun))
    
    #using plyr
    library(plyr)
    
    qdat <- ddply(dat, .(strata), mutate, qq = qfun(var1))
    
    #using data.table (my preference)
    
    
    dat[, qq := qfun(var1), by = strata]
    

    Aggregate usually implies returning an object that is smaller that the original. (inthis case you were getting a data.frame where x was a list of 1 element for each strata.

    0 讨论(0)
提交回复
热议问题