Bin data into quantiles using cut - getting missing values

前端 未结 1 1372
清酒与你
清酒与你 2021-01-26 07:59

I have a dataframe:

a <- matrix(c(1,2,3,4), 2,2)
colnames(a) <- c(\"a\", \"b\")
df <- as.data.frame(a)

> df
  a b
1 1 3
2 2 4

Fir

相关标签:
1条回答
  • 2021-01-26 08:46

    The two values are not in range. You can circumvent this by making another break. I chose Inf, you can use a finite value.

    cat.b <- cut(df$b, c(-Inf, quantile(df$a), Inf))
    #next line can be done better, but illustrates the purpose
    levels(cat.b)[length(levels(cat.b))] <- ">2" 
    levels(cat.b)[1] <- "<1"
    
    cat.b
    [1] >2 >2
    Levels: (1,1.25] (1.25,1.5] (1.5,1.75] (1.75,2] >2
    
    0 讨论(0)
提交回复
热议问题