Nested 'ifelse'-statement for quantiles

后端 未结 2 1780
后悔当初
后悔当初 2021-01-26 05:37

I am attempting to assign a number from 1 through 10 to a series of vectors based on what quantile they\'re in in a dataframe.

So far I have tried

quants         


        
2条回答
  •  清歌不尽
    2021-01-26 06:07

    You might be better off using cut rather than a loop:

    Data = data.frame(Avg = runif(100))
    quantpoints <- seq(0.1, 0.9, 0.1)
    quants <- quantile(Data$Avg, quantpoints)
    
    cutpoints <- c(-Inf, quants, Inf)
    
    cut(Data$Avg, breaks = cutpoints, labels = seq(1, length(cutpoints) - 1))
    

提交回复
热议问题