Introduction to R: Exercises


2. Defining variables. You are given a bond dataset that contains five variables: rate, rating, default, company, and risk_free. Define two new variables: id and excess_return.

The variable id is defined by concatenating the company and rating vectors using the paste() function.

The variable excess_return is defined by subtracting risk_free from rate.


risk_free <- c(2.02, 2.02) rate <- c(11.05, 3.01) rating <- c("C","AA") default <- c(TRUE, FALSE) company <- c("Reckless_Corp", "Cautious_Corp") # Concatenate company and rating. paste(company, ___) # Repeat first step; assign output to id. ___ <- ___(___, ___) # Subtract risk_free from rate. ___ - ___ # Compute excess return and assign it to id. excess_return <- ___ # Concatenate company and rating. paste(company, rating) # Repeat first step; assign output to id. id <- paste(company, rating) # Subtract risk_free from rate. rate - risk_free # Repeat first step; assign output to id. excess_return <- rate - risk_free test_error() test_object("id", incorrect_msg="Recall that you should use the function `paste()` and the variables `company` and `rating` to define `id`.") test_object("excess_return", incorrect_msg="Did you define `excess_return` correctly?") success_msg("Good job! Try printing id to the console to see how company and rating were concatenated.")
Use the paste() function to concatenate strings across two vectors.

Previous Exercise Next Exercise