Test if a vector contains a given element

后端 未结 7 1380
慢半拍i
慢半拍i 2020-11-22 02:40

How to check if a vector contains a given value?

7条回答
  •  感情败类
    2020-11-22 03:00

    Both the match() (returns the first appearance) and %in% (returns a Boolean) functions are designed for this.

    v <- c('a','b','c','e')
    
    'b' %in% v
    ## returns TRUE
    
    match('b',v)
    ## returns the first location of 'b', in this case: 2
    

提交回复
热议问题