【问题标题】:Comparing two lists of values of different lengthsComparing two lists of values of different lengths
【发布时间】:2022-12-02 01:10:03
【问题描述】:

I have a long list of random numbers between 1 and 100, and i would like to count how many of them are larger than 10,20,30 etc

x <- c(sample(1:100, 500, replace = T))
y <- seq(0,100, by = 10)

I am looking for this to return an output such as;

Total 10 20 30 40 50
Count 7 13 17 28 42

Where Count is the number of x Values that are larger than Total (each y value )

So far, I have tried

Count = ifelse(x > y, 1, 0)

However this returns a list of Binary 1,0 returns for each of the 500 values of X

I'd appreciate any help

【问题讨论】:

    标签: r


    【解决方案1】:

    cut + table are useful here:

     table(cut(x, breaks = y))
    
      (0,10]  (10,20]  (20,30]  (30,40]  (40,50]  (50,60]  (60,70]  (70,80]  (80,90] (90,100] 
          51       66       36       44       54       49       55       46       58       41 
    

    Data

    set.seed(505)
    x <- c(sample(1:100, 500, replace = T))
    y <- seq(0,100, by = 10)
    

    【讨论】:

      猜你喜欢
      • 2022-12-01
      • 1970-01-01
      • 2022-12-01
      • 1970-01-01
      • 2014-06-05
      • 1970-01-01
      • 2023-02-24
      • 2021-06-22
      • 1970-01-01
      相关资源
      最近更新 更多