3. Wk2_DataTypesVectorsAndSubsets2013

lengthfweight 1 14 headfweight 1 175 125 185

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: stands for “Not Available” •  NA can be an element of a vector of any type •  NA is different from the character string “NA” •  You can check for the presence of NA values using the is.na() func6on. Special Values •  Other special values are NaN, for “not a number,” which typically arises when you try to compute an indeterminate form such as 0/0. > 0/0 [1] NaN •  The result of dividing a non- zero number by zero is Inf (or -Inf). > 12/0 [1] Inf Special Values •  NULL is a special value that denotes an empty vector > names(fweight) NULL •  Here we asked for the names of the elements of the vector fweight. The func6on names returns a character vector of element names. Since this vector has no element names, the return value is a NULL vector Finding out more informa6on •  Retrieve the number of elements in the vector •  Examine the first 6 elements in the vector •  Elements can have names – height has names •  Are any of the elements in the vector missing? > length(fweight) [1] 14 > head(fweight) [1] 175 125 185 156 105 190 > names(4eight) [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n” > is.na(fweight) [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE … Finding out more informa6on •  Aggregator func6ons operate on the elements of the vector •  Func6ons can tell us the about the data type •  Check if a vector is empty •  Convert a vector to a specified data type > min(fweight) [1] 105 > is.logical(fweight) [1] FALSE > is.null(4eight) [1] FALSE > as.numeric(fgender) [1] 1 2 1 1 2 2 1 2 1 1 2 1 1 2 How to manage variables in the workspace •  Give names of all variables •  Remove one or more variables •  Save objects for future use •  Restore saved variables •  Save an en6re workspace, and it will automa6cally load when you start R again > objects() [1] "age" "bmi" "desiredWt” … > rm(x) > save(age, bmi, desiredWt, weight, height, gender, file="cdc200.rda") > load("cdc200.rda") > q() Save workspace image? [y/n/c]: BUT IT KEEPS EVERYTHING!! Subse4ng Suppose we want the: •  BMI of the 10th person in the family > umi[10] Subset by posiFon [1] 30.04911 •  Ages of all but the first person in the family > fage[- 1] [1] 33 79 47 27 33 67 52 59 27 55 24 46 48 Subset by exclusion Suppose we want the: •  Height of person “j” > veight["j"] S...
View Full Document

Ask a homework question - tutors are online