3. Wk2_DataTypesVectorsAndSubsets2013

# 0 0 0 0 0 2 2 2 2 2 4 4 4 4 4 6 6 6 6 6 8 8 8 8 8

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: the truth or falsity of statements. •  It has three opera6ons, “not,” “or,” and “and.” •  Boolean algebra tells us how to evaluate the truth or falsity of compound statements that are built using these opera6ons. For example, if A and B are statements, some compound statements are •  A and B •  (not A) or B •  The “not” opera6on just causes the statement following it to switch its truth value. So not TRUE is FALSE and not FALSE is TRUE. •  The compound statement A and B is TRUE only if both A and B are TRUE. •  The compound statement A or B is TRUE if either or both A or B is TRUE. •  In R, we write ! for “not,” &amp; for “and,” and | for “or.” Note: all of these are vectorized! &gt; !(fweight &gt; 150)! [1] FALSE TRUE FALSE FALSE [8] TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE TRUE! ! &gt; (fweight &gt; 150) &amp; (fnames == &quot;Tom&quot;)! [1] TRUE FALSE FALSE FALSE FALSE FALSE FALSE ! [8] FALSE FALSE TRUE FALSE FALSE FALSE FALSE! &gt; (fweight &gt; 150) | (fage &gt; 65)! [1] TRUE FALSE TRUE TRUE FALSE TRUE TRUE ! [8] FALSE TRUE TRUE TRUE FALSE FALSE FALSE! Two other func6ons •  Two other useful func6ons that operate on logical vectors are all and any. •  Can you guess what they do? &gt; all(fage &gt; 18)! [1] TRUE! &gt; any(fage &lt; 18)! [1] FALSE! &gt; any(fweight &lt; 150)! [1] TRUE! &gt; all(fweight &lt; 150)! [1] FALSE! Examples •  Under 50 •  Women •  Not over weight fage &lt; 50! ! fgender == “f”! ! !foverWt! ! •  Males who are 70 in tall (fgender == “m”) &amp;(fheight &lt;70)! Use logical expressions to obtain the following subsets •  Ages of all non- overweight members of the family fage[ !foverWt ] •  Genders of those over 50 fgender[ fage &gt; 50 ] •  BMI of the tallest member of the family umi[ veight == max(veight) ] Crea6ng vectors Many func6ons available •  c() - catenate vectors and values together •  : - create a sequence of values 1 apart •  seq() – create more complex sequences •  rep() – repeat values in a vector •  sort() – sort the values in a vector •  order() – provide the order of values Let’s show how they work by example concatenate &gt; c(3, 2, 1) [1] 3 2 1 &gt; c(2,3,1) [1] 2 3 1 &gt; x = c(bob =3, alice = 2, john = 1) &gt; x bob alice john 3 2 1 •  A vector of three numbers, 3, 2, 1, in that order •  A diﬀerent vector with the same values i...
View Full Document

Ask a homework question - tutors are online