3. Wk2_DataTypesVectorsAndSubsets2013

fweight 150 fnames tom 1 true false false

Info icon This 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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern