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

后端 未结 19 1355
闹比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:45

    You can make a function to give you results.

    # your list
    numbers <- c(4,23,4,23,5,43,54,56,657,67,67,435,
             453,435,324,34,456,56,567,65,34,435)
    
    function1<-function(x){
        if(x==value){return(1)}else{ return(0) }
    }
    
    # set your value here
    value<-4
    
    # make a vector which return 1 if it equal to your value, 0 else
    vector<-sapply(numbers,function(x) function1(x))
    sum(vector)
    

    result: 2

    0 讨论(0)
提交回复
热议问题