3. Wk2_DataTypesVectorsAndSubsets2013

fweight 150 fnames tom 1 true false false

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: d to bexer understand: •  How to use logical operators to create logical vectors •  How to create vectors with specific numbers and/or lexers Review: subse4ng vectors Subset by posi6on age position 1 3 7 8 73 33 79 47 27 33 67 52 59 27 55 24 46 48 subset 73 79 67 52 Subset by posi6on age position 1 8 7 3 73 33 79 47 27 33 67 52 59 27 55 24 46 48 subset 73 52 67 79 Subset by exclusion age exclusion -2 -4 -5 -6 -9 -10 -11 -12 -13 -14 73 33 79 47 27 33 67 52 59 27 55 24 46 48 subset 73 79 67 52 Subset by logical logical T F T F F F T T F F F F F F age 73 33 79 47 27 33 67 52 59 27 55 24 46 48 subset 73 79 67 52 Subset by name name "a" "c" "g" "h" name "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" age 73 33 79 47 27 33 67 52 59 27 55 24 46 48 subset 73 79 67 52 Five ways to subset a vector •  Posi6on – indices of the element you want •  Exclusion – indices of elements to exclude •  Logical – logical vector the same length as the vector being subset. Keep the elements corresponding to TRUE. •  Name – character vector of names of elements to keep. Vector being subsexed must have names associated with elements •  All – all the elements Logical Opera6ons Logical/Rela6onal Operators •  In addi6on to operators such as +, - , *, and / R also has logical operators •  They are rela6onal operators >, <, >=, <=, !=, and ==! •  These return a value of TRUE or FALSE •  They are also vectorized opera6ons Examples > 4 < 3! [1] FALSE! > "a" == "A"! [1] FALSE! > "A" == "A"! [1] TRUE! > 4 != 3! [1] TRUE! > fweight > 150! [1] TRUE FALSE TRUE TRUE FALSE TRUE [9] TRUE TRUE TRUE FALSE FALSE FALSE! TRUE FALSE > fgender !="m"! [1] FALSE TRUE FALSE FALSE TRUE TRUE FALSE [9] FALSE FALSE TRUE FALSE FALSE TRUE! TRUE > fbmi! [1] 25.16239 21.50106 24.45884 24.48414 18.06089 [6] 28.94981 28.18797 20.67783 26.66430 30.04911 ! [11] 26.05364 22.64384 24.26126 22.91060! > fbmi == 25.16239! [1] FALSE FALSE FALSE FALSE FALSE FALSE …! ! Weights of the women in our family •  Create a logical expression that iden6fies the women in the family > fgender == "f"! [1] FALSE TRUE FALSE FALSE TRUE TRUE FALSE [8] TRUE FALSE FALSE TRUE FALSE FALSE TRUE! •  Use this logical expression to subset the vector of fweight > fweight[ fgender == "f"]! [1] 125 105 190 124 166 125! ! Boolean Algebra •  Boolean algebra is a mathema6cal formaliza6on of...
View Full Document

This document was uploaded on 02/16/2014 for the course STATISTICS 3026 at Columbia.

Ask a homework question - tutors are online