七度&光 2009-12-17 17:21 采纳率: 22.2%
浏览 286
已采纳

用向量中 x 的值计算元素个数

I have a vector of numbers:

numbers <- c(4,23,4,23,5,43,54,56,657,67,67,435,
         453,435,324,34,456,56,567,65,34,435)

How can I have R count the number of times a value x appears in the vector?

转载于:https://stackoverflow.com/questions/1923273/counting-the-number-of-elements-with-the-values-of-x-in-a-vector

  • 写回答

12条回答 默认 最新

  • derek5. 2009-12-17 17:25
    关注

    You can just use table():

    > a <- table(numbers)
    > a
    numbers
      4   5  23  34  43  54  56  65  67 324 435 453 456 567 657 
      2   1   2   2   1   1   2   1   2   1   3   1   1   1   1 
    

    Then you can subset it:

    > a[names(a)==435]
    435 
      3
    

    Or convert it into a data.frame if you're more comfortable working with that:

    > as.data.frame(table(numbers))
       numbers Freq
    1        4    2
    2        5    1
    3       23    2
    4       34    2
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(11条)

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题