Delete a vector element with% in% returns the character (0)

Got a quick question. I am trying to remove a vector element using the code below. But I return character(0)instead of the rest of the elements of the vector.

What did I do wrong?

> str(ticker.names)
 chr [1:10] "AAK.ST" "ABB.ST" "ALFA.ST" "ALIV-SDB.ST" "AOI.ST" "ASSA-B.ST" "ATCO-A.ST" "ATCO-B.ST" "AXFO.ST" "AXIS.ST"
> ticker.names[! 'AAK.ST' %in% ticker.names]
character(0)
+4
source share
1 answer

If we need to remove items in `ticker.names' that are not 'AAK.ST'.

ticker.names[!ticker.names %in% 'AAK.ST']

Or use setdiff

setdiff(ticker.names, 'AAK.ST')

Consider an approach that uses the OP,

'AAK.ST' %in% ticker.names
#[1] TRUE
ticker.names['AAK.ST' %in% ticker.names]
#[1] "AAK.ST"  "ABB.ST"  "ALFA.ST"

Denying

!'AAK.ST' %in% ticker.names
#[1] FALSE

ticker.names[!'AAK.ST' %in% ticker.names]
#character(0)

In the former case, TRUE"ticker.names" returns to the length, therefore all elements of the vector FALSEare returned , while in the latter, it receives recycling and no elements are returned.

data

ticker.names <- c('AAK.ST', 'ABB.ST', 'ALFA.ST')
+7
source

Source: https://habr.com/ru/post/1621719/


All Articles