The difference between & and && in R

后端 未结 3 1930
鱼传尺愫
鱼传尺愫 2020-12-11 19:56

I have read

http://stat.ethz.ch/R-manual/R-devel/library/base/html/Logic.html

and the difference between & and && doesn\'t make sense. For examp

3条回答
  •  臣服心动
    2020-12-11 20:17

    Consider this, then it should be clear:

    as.logical(c(0,1,2,3,4))
    #[1] FALSE  TRUE  TRUE  TRUE  TRUE
    

    So...

    c(1,2,3) & c(1,3,3)
    #[1] TRUE TRUE TRUE
    

    is equivalent to:

    c(TRUE,TRUE,TRUE) & c(TRUE,TRUE,TRUE)
    

    ...which compares by element using & and returns c(TRUE,TRUE,TRUE)

    For reference:

    test <- c(NA,NaN,-Inf,-1,-0.5,0,0.5,1,2,Inf)
    data.frame(test,as.logical(test))
    
    #   test as.logical.test.
    #1    NA               NA
    #2   NaN               NA
    #3  -Inf             TRUE
    #4  -1.0             TRUE
    #5  -0.5             TRUE
    #6   0.0            FALSE
    #7   0.5             TRUE
    #8   1.0             TRUE
    #9   2.0             TRUE
    #10  Inf             TRUE
    

提交回复
热议问题