Back to Browse

Subset Data Frame Rows by Logical Condition in R (5 Examples) | subset & filter Functions in RStudio

13.3K views
Dec 9, 2019
7:04

How to filter rows of a data frame or tibble by logical condition in the R programming language. More information: https://statisticsglobe.com/filter-data-frame-rows-by-logical-condition-in-r R code of this tutorial: data <- data.frame(x1 = c(3, 7, 1, 8, 5), # Create example data x2 = letters[1:5], group = c("g1", "g2", "g1", "g3", "g1")) ##### Example 1 data[data$group == "g1", ] # Subset rows with == ##### Example 2 data[data$group != "g1", ] # Subset rows with != ##### Example 3 data[data$group %in% c("g1", "g3"), ] # Subset rows with %in% ##### Example 4 subset(data, group == "g1") # Apply subset function ##### Example 5 install.packages("dplyr") # Install dplyr package library("dplyr") # Load dplyr package filter(data, group == "g1") # Apply filter function

Download

0 formats

No download links available.

Subset Data Frame Rows by Logical Condition in R (5 Examples) | subset & filter Functions in RStudio | NatokHD