Nested 'ifelse'-statement for quantiles

后端 未结 2 1781
后悔当初
后悔当初 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:08

    This should work:

    Data$quant <- for ( i in nrow(Data) ) {
      Data$quant[1] <- ifelse(Data$Avg [i] < quants[1],  1, ifelse(Data$Avg [i] > quants[1] & Data$Avg[i] < quants[2], 2, 3))
    }
    

    Or equivalently (inside the for loop):

    if(Data$Avg [i] < quants[1])
        Data$quant[1] <- 1
    else{
        if(Data$Avg [i] > quants[1] & Data$Avg[i] < quants[2])
            Data$quant[1] <- 2
        else
            Data$quant[1] <- 3
    }
    

    You should assign the output of ifelse conditions outside of it. That is:

    output <- ifelse(a > b, a, b)
    

提交回复
热议问题