# Part (a)
flavors <- c("Cookies & Cream", "Americone Dream (R)", "Bob Marley's 1 Love")
flavors
## [1] "Cookies & Cream" "Americone Dream (R)" "Bob Marley's 1 Love"
# Part (b)
length(flavors)
## [1] 3
# Part (c)
nchar(flavors)
## [1] 15 19 19
# Part (d)
toupper(flavors)
## [1] "COOKIES & CREAM" "AMERICONE DREAM (R)" "BOB MARLEY'S 1 LOVE"
tolower(flavors)
## [1] "cookies & cream" "americone dream (r)" "bob marley's 1 love"Lecture 03a Practice
These activities are for practice only — there is nothing to turn in. Answer keys are included, so try each question yourself before opening them.
Question 1
- Create a character vector named
flavorsthat contains the following strings and print it to the console:
- Cookies & Cream
- Americone Dream (R)
- Bob Marley’s 1 Love
- Use a function to calculate the number of strings in
flavors. - Use another function to calculate the number of characters in each string in
flavors. - Let R know how you feel about ice cream by using a function to either make all the characters in
flavorsuppercase (you LOVE ice cream) or lowercase (you don’t love ice cream).
Answer key
Question 2
Ebi has tracked her spending over two weeks. Every time she makes a purchase on items that are not food, she writes down a code representing the day of the week (e.g., 1 = Sunday and 2 = Monday). At the end of the week, she has the following codes written down: 1 2 2 4 7 1 1 5 6 7.
- Create a numerical vector (not a factor) called
ebi_numthat contains these codes. Print it to the console. - Turn
ebi_numinto a factor object calledebi_fctwith levels 1, 2, …, 7 and labels “Sun”, “Mon”, …, “Sat”. Print it to the console. - Use the
table()function onebi_numand then onebi_fct. Compare their output. Which version do you prefer and why?
Answer key
Answer (a)
ebi_num <- c(1, 2, 2, 4, 7, 1, 1, 5, 6, 7) ebi_num[1] 1 2 2 4 7 1 1 5 6 7Answer (b)
ebi_fct <- factor( ebi_num, levels = c(1, 2, 3, 4, 5, 6, 7), labels = c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat") ) ebi_fct[1] Sun Mon Mon Wed Sat Sun Sun Thu Fri Sat Levels: Sun Mon Tue Wed Thu Fri SatAnswer (c)
table(ebi_num)ebi_num 1 2 4 5 6 7 3 2 1 1 1 2table(ebi_fct)ebi_fct Sun Mon Tue Wed Thu Fri Sat 3 2 0 1 1 1 2I prefer the factor version because it includes the labels at the top, which is convenient, and it also includes the zero count for “Tue”, whereas the numerical version omits it. This makes it easier to see that Tuesdays had the fewest purchases for Ebi.
Question 3
- Install the
tidyversepackage. It may take several minutes to install because it contains several sub-packages that we will use in this class. - Check if the
tidyversepackage has a website and/or vignettes. Read the “Welcome to the tidyverse” vignette (it is relatively brief and will prepare you for the next chapter).
Answer key
# Part (a) install.packages("tidyverse") # or RStudio > Extras pane > Packages tab > Install button # Part (b) browseVignettes("tidyverse") # or RStudio > Extras Pane > Packages tab > search for tidyverse > click globe icon > check website for Articles
Note that there is no need to turn in Activities. These are just for practice!