Write R code to create a data frame called products with columns: product = c("Laptop", "Tablet", "Phone"), price = c(1200, 600, 800), and in_stock = c(TRUE, TRUE, FALSE). Then extract the price column using $.
Data frames are one of the most commonly used data structures in R for storing tabular data. Each column can have a different type, such as numeric, character, or factor.
students <- data.frame(
name = c("Alice", "Bob", "Charlie"),
age = c(23, 22, 24),
grade = c("A", "B", "A")
)
$
to access a column: students$name
[row, column]
indexing: students[1, 2]
head()
to view the first rows: head(students)
Tibbles are modern versions of data frames (from the tibble or tidyverse package) with better printing and subsetting behavior:
library(tibble)
students_tb <- tibble(
name = c("Alice", "Bob", "Charlie"),
age = c(23, 22, 24),
grade = c("A", "B", "A")
)
Tibbles are especially helpful when working with large datasets in the tidyverse ecosystem.