Lesson 13 of 16

Data Frames

Data Frames

A data frame is R's primary structure for tabular data. Each column is a vector, and columns can have different types:

df <- data.frame(
  name = c("Alice", "Bob", "Charlie"),
  age = c(25, 30, 35),
  active = c(TRUE, FALSE, TRUE)
)

Accessing Columns

Use $ to access a column by name:

cat(df$name, "\n")   # Alice Bob Charlie
cat(df$age, "\n")    # 25 30 35

Dimensions

cat(nrow(df), "\n")   # 3 (number of rows)
cat(ncol(df), "\n")   # 3 (number of columns)

Indexing

Use [row, col] notation, similar to matrices:

cat(df[1, 2], "\n")      # 25 (row 1, col 2)
cat(df[2, "name"], "\n") # Bob (row 2, column "name")

Adding Columns

df$score <- c(90, 85, 92)  # Add a new column

Filtering Rows

young <- df[df$age < 30, ]
# Returns rows where age < 30

Your Task

Create a data frame with columns language ("R", "Python", "Julia") and year (1993, 1991, 2012). Print the language column, then print the number of rows.

R runtime loading...
Loading...
Click "Run" to execute your code.