Introduction to R: Exercises


7. Defining a data frame. In R, it will often be simpler to work with data in the form of a data frame, rather than a collection of vectors. In this exercise, you will combine bond data, which consists of two variables -- rate, and company -- into a data frame object.

You can define a data frame by passing the data.frame() function column names and associated vectors. For example, the code below defines a data frame with the columns X and Y.

data <- data.frame(X = c(1,2,3), Y = c(4,5,6))


rate <- c(11.05, 3.01) rating <- c("C","AA") default <- c(TRUE, FALSE) company <- c("Reckless_Corp", "Cautious_Corp") # Define a data frame using rate and company. bond_data <- ___( bond_rate = ___, bond_company = ___ ) # Print data frame. print(___) # Define a data frame using rate and company. bond_data <- data.frame( bond_rate = rate, bond_company = company ) # Print data frame. print(bond_data) test_error() test_object("bond_data", incorrect_msg="Remember to use the `data.frame()` function and supply the correct vector for each column.") test_output_contains("print(bond_data)", incorrect_msg="Did you print the correct object?") success_msg("Excellent work!")
Check that you have applied the right function and used the right vectors.

Previous Exercise Next Exercise