How logical negation operator “!” works

后端 未结 3 1202
孤独总比滥情好
孤独总比滥情好 2021-01-11 15:20

I am not trying to solve any particular problem, but trying to learn R and understand its logical negation operator \"!\" documented on page http://stat.ethz.ch/R-manual/R-d

3条回答
  •  星月不相逢
    2021-01-11 15:34

    First, it's probably best not to think of != as ! acting on =, but rather as a separate binary operator altogether.

    In general, ! should only be applied to boolean vectors. So this is probably more like what you are after:

    vector1 <- 1:5 # just making vector of 5 numbers
    vector2 <- 5:1 # same vector backwards
    l <- list(Forward=vector1, Backwards=vector2) # producing list with two elements
    x = "Forward"
    l[!(names(l) %in% x)]
    

    where names(l) %in% x returns a boolean vector along the names of the list l indicating whether they are contained in x or not. Finally, I avoided the use of list as a variable, since you can see it is a fairly common function as well.

提交回复
热议问题