In R, a data frame is like a table or a spreadsheet in which you can store data. It's a two-dimensional data structure where rows represent observations and columns represent variables or attributes.
Let's understand data frames with an example:
Example
# Creating a data frame
my_df <- data.frame(
Name = c("John", "Alice", "Bob"),
Age = c(30, 25, 35),
Gender = c("Male", "Female", "Male"),
Married = c(TRUE, FALSE, TRUE)
)
# Printing the data frame
print(my_df)
In this example:
data.frame()
function to create a data frame.my_df
.The resulting data frame will look like this:
Example
Name Age Gender Married
1 John 30 Male TRUE
2 Alice 25 Female FALSE
3 Bob 35 Male TRUE
Each row represents an observation (e.g., a person in this case), and each column represents a variable or attribute (e.g., name, age, gender, marital status).
Data frames are widely used for data analysis, manipulation, and visualization in R. They provide a convenient way to work with structured data, such as data imported from spreadsheets, databases, or other sources.