Difficulty: beginner
Estimated Time: 10 minutes

When working with the data, sooner or later (rather sooner) you need a way to choose a subset. It can be based on some logical condition, specific index or some sampling technique.

This scenario provides you with R capabilities of filtering data from simple collections to data frames.

You've completed Filtering data in R scenario.

You've learnt how to:

  • define logical condition
  • indexing
  • filtering

Find out about working with files in the in the next scenario of the course.

Filtering data in R

Step 1 of 7

Logical conditions

When working with the vector data there is a simple way to use logical condition to filter it.

Let us create a vector of sequential numbers:

a <- 1:10 a

To get only values larger than 4 [] should be used for the condition:

a[a > 4]

The same rule could be applied to the char vector and the condition of strings of the length 2.

b <- c("ab", "bc", "abc", "ac", "def") b b[nchar(b) == 2]

Conditions and parenthesis can be used for lists too. Just remember that because list can store elements of different types, the condition needs to make sense for all of them.

A <- as.list(1:10) A A[A > 4]