What is the behavior of the ampersand operator in R's sum(...) function -
below, line script i'm translating r python. i'm more experienced @ python @ r, , i'm running little trouble here:
val = sum(l & f==v)
let l
vector of true/false values. let f
vector of trivial values, , v
possible value of f
test against. expect l
, f
of same length. f==v
part yield boolean array. left question &
/ampersand (logical and, according r documentation) in context. sum()
function return sum of boolean array indicates both l
, f==v
boolean arrays true? or wil sum true values both arrays , add them up?
thank in advance!
let define several vectors :
l <- c(true, false, true, false, true) v <- 1:5 f <- rep(c(1, 4), c(3, 2))
now let see have when decompose line sum(l & f==v)
:
in line, ==
has precedence on &
:
fev <- f==v fev [1] true false false true false
then l & fev
:
lafev <- l & fev [1] true false false false false
lastly, sum:
sum(lafev) [1] 1
the sum tells how many simultaneous true
there in l
, f==v
converting logical
values numeric: true
becomes 1
, false
becomes 0
. so, in example, 1.
Comments
Post a Comment