0

I am trying to filter out all FALSE entries in a dataframe I've created using the mtcars dataset. I have tried using filter function but I do not understand it, and I've tried is.na function but I couldn't get this to work.

Below is how I've created the dataframe

James_Test <- data.frame(rownames(mtcars), mtcars$mpg, mtcars$mpg > 15)
James_Test_names <- c("Model", "MPG", "MPG > 15")
colnames(James_Test) <- James_Test_names
head(James_Test, 7)

#               Model  MPG MPG > 15
# 1         Mazda RX4 21.0     TRUE
# 2     Mazda RX4 Wag 21.0     TRUE
# 3        Datsun 710 22.8     TRUE
# 4    Hornet 4 Drive 21.4     TRUE
# 5 Hornet Sportabout 18.7     TRUE
# 6           Valiant 18.1     TRUE
# 7        Duster 360 14.3    FALSE

I would like to be only left with the TRUE result and none of the FALSE entries.

Artem
  • 3,304
  • 3
  • 18
  • 41
  • 1
    `James_Test[James_Test$\`MPG > 15\`,]` – d.b Aug 01 '19 at 19:28
  • Perhaps this would help. It shows a subset based on an inequality and compares two most popular basic methods. https://stackoverflow.com/questions/9860090/why-is-better-than-subset –  Aug 01 '19 at 19:40
  • Alternatively: `James_Test <- data.frame(Model=row.names(mtcars)[mtcars$mpg>15], MPG=mtcars$mpg[mtcars$mpg>15], MPG_Over_15=TRUE)` – Parfait Aug 01 '19 at 19:50
  • If you just want rows with MPG > 15, you theoretically don't even need the `MPG > 15` column. Using `dplyr` you can just do: `dplyr::filter(James_Test, MGP > 15)`. This will filter the data so it only shows rows there MPG is gr eater than 15. – ColinB Aug 01 '19 at 21:05
  • Just use tidy verse. `James_Test %>% filter(`MPG > 15` == TRUE)` – der_radler Aug 01 '19 at 21:40

1 Answers1

0

From dplyr package:

filter(James_Test, `MPG > 15` == TRUE)
Alberson Miranda
  • 1,248
  • 7
  • 25