O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Selecting using logical vector

You can specify the cases (rows) and variables you want to retrieve using Boolean vectors.

In the following example, R returns the first and second rows, and for each row, we only get the third variable column; we get the third variable alone. R returns the elements corresponding to TRUE:

> auto[1:2,c(FALSE,FALSE,TRUE)] 

You can use the same approach for rows also.

If the lengths do not match, R recycles through the Boolean vector. However, it is always good practice to match the size.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required