100%(1)1 out of 1 people found this document helpful
This preview shows pages 1–4. Sign up to view the full content.
When you print some_vector, there will be title labels above each element○notes(some_vector)<- c("name1","name2") assigns names to elements of some_vector•Always try to avoid code duplication•Adding two vectors adds respective elements•sum(some_vector)adds all elements of a vector•positive_vector <- some_vector > 0 will create vector with boolean values (T/F)indicating whether each element •passes the condition (elem > 0)another_vector[boolean_vector]will only return elements that correspond withTRUEin boolean vector•rowSums(matrix)adds all elements of row and makes new column matrix•DataCamp: Intro to RTuesday, February 21, 20173:53 PMCSC121 Page 1
has intentionally blurred sections.
Sign up to view the full version.
Data type that stores categorical variables•Important because R needs to know whether var is categorical or continuous (numerical)•Create vector (category_vector) with all categories aka factor levels1.factor_category_vector <- factor(category_vector)encodes vector as a factor2.Setting Up Factors# of categories in a factor = # of "factor levels"•Recall: nominal means no implied order, ordinal means natural order exists> survey_vector <- c("M", "F", "F", "M", "M")> factor_survey_vector <- factor(survey_vector)> levels(factor_survey_vector) <- c("Female", "Male")Note: "Female" comes first because F is alphabetically before M, so it will begin naming from F firstWhen assigning levels to a factor (changing names of categories), your level vector c("name1","name2") should correspond to the alphabetical order of your factor•summary(some_vector) that is NOT factorized, will only give you info about what kind of string it is•summary(factor_some_vector) allows R to tally up occurences of each factor level•factor() creates unordered factor•Need to add two arguments to make it ordinal: ordered and levels•factor(some_vector,ordered = TRUE,levels = c("lev1", "lev2" ...))where levels allows us to give values of factor in correct orderNow printing the factor vector tells us levels, and summary tells us number of occurences•Ordered FactorsElements of an ordered factor can be tested for inequalites (> = <), resulting in T/F•FactorsTuesday, February 21, 20173:53 PMCSC121 Page 2