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

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

View Full Document Right Arrow Icon
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,” & for “and,” and | for “or.” Note: all of these are vectorized! > !(fweight > 150)! [1] FALSE TRUE FALSE FALSE [8] TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE TRUE! ! > (fweight > 150) & (fnames == "Tom")! [1] TRUE FALSE FALSE FALSE FALSE FALSE FALSE ! [8] FALSE FALSE TRUE FALSE FALSE FALSE FALSE! > (fweight > 150) | (fage > 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? > all(fage > 18)! [1] TRUE! > any(fage < 18)! [1] FALSE! > any(fweight < 150)! [1] TRUE! > all(fweight < 150)! [1] FALSE! Examples •  Under 50 •  Women •  Not over weight fage < 50! ! fgender == “f”! ! !foverWt! ! •  Males who are 70 in tall (fgender == “m”) &(fheight <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 > 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 > c(3, 2, 1) [1] 3 2 1 > c(2,3,1) [1] 2 3 1 > x = c(bob =3, alice = 2, john = 1) > x bob alice john 3 2 1 •  A vector of three numbers, 3, 2, 1, in that order •  A different vector with the same values i...
View Full Document

Ask a homework question - tutors are online