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 conditio
n (elem > 0)
another_vector[boolean_vector]
will
only return
elements that correspond with
TRUE
in boolean vector
•
rowSums(matrix)
adds all elements of row and makes new column matrix
•
DataCamp: Intro to R
Tuesday, February 21, 2017
3:53 PM
CSC121 Page 1

This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*
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 levels
1.
factor_category_vector
<- factor
(category_vector)
encodes vector as a factor
2.
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 first
When 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 order
Now printing the factor vector tells us levels, and summary tells us number of occurences
•
Ordered Factors
Elements of an ordered factor can be tested for inequalites (> = <), resulting in T/F
•
Factors
Tuesday, February 21, 2017
3:53 PM
CSC121 Page 2