Search⌘ K
AI Features

Solution Review: Data Frames

Explore how to construct data frames in R by combining vectors of different data types using the data.frame function. Understand the importance of using print over cat when displaying data frames to correctly output mixed data types.

We'll cover the following...

Solution: Using data.frame()

R
Course <- c("Maths", "English", "Science")
LecturesTaken <- c("12", "49", "52")
Grade <- c('F', 'B', 'A')
studentDataframe <- data.frame(Course, LecturesTaken, Grade)
print(studentDataframe)

Explanation

The solution to this exercise is simple. We first create vectors of Course, LecturesTaken, and Grade, according to the given data. Later we pass the three vectors to data.frame() and simply print it.

Note: We cannot use cat() here because cat() takes objects that contain data of only one type. But a data frame for example in this exercise contains data of multiple types: character, integer, etc. This is why we use only print() here.