Exercise: Make a vector

Start by making a vector, named x, with the numbers 1 through 26.

Multiply the vector by 2.

Give the resulting vector names A through Z. Hint: see the documentation page for names for an example. Hint 2: there is a built in vector called LETTERS

Print the vector out with the elements in reverse order. Hint: you don’t need a function to do this (although there is one that will); you can do it based on how you specify the indices to select from the vector.

Exercise: Manipulating Vectors

Set the 10th and 13th elements of the x vector you made in the last exercise to missing.

Then select every other element, starting with the second, of the vector. See the help for seq to help with this.

Take the mean of the log of the values in x. Exclude missing values.

Make a second vector badvals that has the values 4, 6, 8, and 10 in it. Use it to select the values of x that aren’t in badvals. Hint: remember what the not operator is? Put it at the beginning of the expression inside the []. You may also want to look up the help page for match if you don’t remember how to tell if values are in another vector.

Exercise: Looking up Values

The code below generates a vector with randomly selected (lowercase) letters. Check to see if your first initial is in the vector. Which index positions is it at?

Make a table of the frequencies of the letters in random_letters. Sort the table with the sort function.

Challenge: Replace occurences of the most frequent letter in random_letters with an * instead (don’t worry about any ties). Do this without hard coding in the value of the most frequent letter (meaning, use variables, don’t type the value of the letter). Hint: you’ll probably need to use the names function at some point to get the most frequent letter.

random_letters<-sample(letters, 50, replace=TRUE)

Exercise: rep and seq

Create a vector where A appears 1 time, B appears 2 times, C appears 3 times, etc. for the alphabet. How long is the resulting vector?

Exercise: Joining Strings

To concatenate (join) character (string, text) data, use the paste function. The default text that’s put between the strings when they are joined is a space, but you can change this with the sep argument. paste0 pastes strings together with no separator.

paste("Ahmad", "Haddad", sep=" ")
## [1] "Ahmad Haddad"

Create a variable with your age. Use paste to create a sentence with your age in it.

Exercise: Workspace management

Remove the x vector you created above from your workspace. Check to make sure it’s gone.

Exercise: Packages

Install the package fortunes. Look at the package documentation to figure out what it does and call the main method of the package.

Challenge Exercise: Working with Strings

Part 1: Create a variable called age with your age. Using that variable, output your age as part of a sentence like “My age is 25.” Hint: look up the paste function.

Part 2: Concatenate all of the letters in LETTERS together into a single object, with a “-” between each letter. Hint: you’ll need the collapse argument of paste. Save this result as a new object allletters.

Load the stringr package. Using a function from the stringr package (look at the package documentation to find one), split allletters on “-” so that you have individual letters again, saved in another object. What type of object did you get back after splitting the letters? How can you get this back to a vector?

Part 3 (advanced): Find a function in base R (not the stringr package) that also splits strings (you may need to use Google). Compare the documentation to the function you found above. What’s the difference? Hint: try supplying a vector with different split patterns to each function and see what output you get.

Challenge Exercise: Packages and Strings

Install the cowsay package. Combine the output of the fortunes package (which you installed and used in an exercise above – if not, go back and do that) with the functionality of the cowsay package. Hint: you will also need to use the paste function with the collapse argument. To keep pretty formatting, also use the capture.output function.

Exercise: Factors

Use the code below to create a vector with randomly selected names of months. Then turn this vector into a factor. Create a table of the value frequencies in the vector with the table function.

Then remake the factor by explicitly setting ordered levels. Hint: month.abb is a built in vector that lists the months in order.

Make a table again. What’s the difference?

months<-sample(month.abb, size=40, replace=TRUE)

Challenge Exercise: Dates

Use R to calculate how many days you’ve been alive. Then calculate it in terms of weeks. Find out what day of the week you were born on. You’ll need to search the internet to figure out what functions to use, and use the R help as necessary.

Hint: You might want to start with the help page for Date, the class of date objects in R. Alternatively, take a look at the lubridate package.