Rの論理演算子 & と &&、 | と || の違い
【1つのもの】(「&」と「|」) ベクトルのそれぞれの要素を評価 > c(TRUE, TRUE, FALSE, FALSE) & c(TRUE, FALSE, TRUE, FALSE) [1] TRUE FALSE FALSE FALSE 【2つのもの】(「&&」と「||」) ベクトルの1つ目の要素のみ評価 > c( TRUE , TRUE, FALSE, FALSE) && c( TRUE , FALSE, TRUE, FALSE) [1] TRUE 前者はRの演算子や関数ではおなじみの挙動ですよね。 この動きについては、ヘルプを見れば書いてあります。呼び出し方は以下↓ > ?"&" starting httpd help server ... done 訳は私によるアバウトなもの。 & and && indicate logical AND and | and || indicate logical OR. 「&」と「&&」は論理積(AND)を、 「|」と「||」は論理和(OR)を表します。 The shorter form performs elementwise comparisons in much the same way as arithmetic operators. 短い方の形式は、他の算術演算子と同じように、要素ごとの比較を行う。 The longer form evaluates left to right examining only the first element of each vector. 長い方の形式は、左から右へと評価を行い、両者のベクトルの最初の要素のみを比較する。 Evaluation proceeds only until the result is determined. 評価は結果が確定するまでしか行われない。 The longer form is appropriate for programming control-flow and typically preferred in if clauses. 長い方の形...