Logical Indexing
Logical indexing allows you to filter rows of a data frame based on conditions. Instead of remembering row numbers, you specify logical expressions, and only rows where the condition is TRUE are returned.
Example
123456789name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") test <- data.frame(name, age, gender) # People older than 30 test[test$age > 30, ] # Males only test[test$gender == 'M', ]
The condition should be placed in the row index position inside square brackets ([condition, ]).
Swipe to start coding
Using the mtcars dataset, extract the following data:
- The cars that pass a quarter-mile in less than 16 seconds (
qseccolumn). - Cars with 6 cylinders (
cylcolumn).
Løsning
Takk for tilbakemeldingene dine!
single
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Fantastisk!
Completion rate forbedret til 2.27
Logical Indexing
Sveip for å vise menyen
Logical indexing allows you to filter rows of a data frame based on conditions. Instead of remembering row numbers, you specify logical expressions, and only rows where the condition is TRUE are returned.
Example
123456789name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") test <- data.frame(name, age, gender) # People older than 30 test[test$age > 30, ] # Males only test[test$gender == 'M', ]
The condition should be placed in the row index position inside square brackets ([condition, ]).
Swipe to start coding
Using the mtcars dataset, extract the following data:
- The cars that pass a quarter-mile in less than 16 seconds (
qseccolumn). - Cars with 6 cylinders (
cylcolumn).
Løsning
Takk for tilbakemeldingene dine!
single