Counting the number of elements with the values of x in a vector

后端 未结 19 1329
闹比i
闹比i 2020-11-22 02:44

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 hav

19条回答
  •  不知归路
    2020-11-22 03:31

    Using table but without comparing with names:

    numbers <- c(4,23,4,23,5,43,54,56,657,67,67,435)
    x <- 67
    numbertable <- table(numbers)
    numbertable[as.character(x)]
    #67 
    # 2 
    

    table is useful when you are using the counts of different elements several times. If you need only one count, use sum(numbers == x)

提交回复
热议问题