Lesson 15 of 16
Data Frame Operations
Filtering with subset()
The subset() function filters rows and selects columns:
df <- data.frame(
name = c("Alice", "Bob", "Charlie", "Diana"),
age = c(25, 30, 35, 28),
score = c(90, 85, 92, 88)
)
young <- subset(df, age < 30)
# Returns rows where age < 30
You can also select specific columns:
result <- subset(df, age >= 30, select = c(name, score))
Sorting with order()
Use order() to sort a data frame:
sorted <- df[order(df$age), ] # Sort by age ascending
sorted <- df[order(-df$age), ] # Sort by age descending
sorted <- df[order(df$age, df$name), ] # Sort by age, then name
Merging Data Frames
Use merge() to join two data frames:
df1 <- data.frame(id = 1:3, name = c("A", "B", "C"))
df2 <- data.frame(id = 2:4, score = c(85, 92, 78))
merged <- merge(df1, df2, by = "id")
# Only matching ids (inner join by default)
Your Task
Create a data frame with columns city ("Paris", "Tokyo", "London", "Sydney") and pop (2.1, 13.9, 8.9, 5.3). Sort it by pop in descending order and print the city column of the sorted result.
R runtime loading...
Loading...
Click "Run" to execute your code.