Test if a vector contains a given element

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

How to check if a vector contains a given value?

7条回答
  •  醉酒成梦
    2020-11-22 02:52

    is.element() makes for more readable code, and is identical to %in%

    v <- c('a','b','c','e')
    
    is.element('b', v)
    'b' %in% v
    ## both return TRUE
    
    is.element('f', v)
    'f' %in% v
    ## both return FALSE
    
    subv <- c('a', 'f')
    subv %in% v
    ## returns a vector TRUE FALSE
    is.element(subv, v)
    ## returns a vector TRUE FALSE
    

提交回复
热议问题