What is an R Data Frame?
a) A two-dimensional data structure that can store elements of different types, similar to a table
b) A multi-dimensional array
c) A single-dimensional vector
d) A method for storing lists
Answer:
a) A two-dimensional data structure that can store elements of different types, similar to a table
Explanation:
A data frame in R is a two-dimensional data structure that can store elements of different types (such as numbers, characters, and factors), similar to a table in a database or a spreadsheet. Each column in a data frame can contain a different type of data, while all rows represent individual observations.
# Creating a data frame in R
my_data_frame <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(25, 30, 35),
Salary = c(50000, 60000, 70000)
)
# Printing the data frame
print(my_data_frame)
# Output:
# Name Age Salary
# 1 Alice 25 50000
# 2 Bob 30 60000
# 3 Charlie 35 70000
In this example, a data frame named my_data_frame
is created with three columns: Name, Age, and Salary. Each column can contain a different type of data, such as character strings for names and numeric values for ages and salaries.
Data frames are one of the most commonly used data structures in R for data analysis, allowing you to manipulate and analyze tabular data efficiently.