We can modify the code without filter
library(dplyr)
cars %>%
slice(which(Cylinder == 4 & Doors == 4)[1])
-output
# Year Car Doors Cylinder Transmission
#1 2003 Nissan 4 4 MT
If we need to extract the 'Transmission', use pull
cars %>%
slice(which(Cylinder == 4 & Doors == 4)[1]) %>%
pull(Transmission)
NOTE: In the filter
, the expression used is assignment operator (=
) instead of comparison operator (==
). Also, R
is case-sensitive i.e. it would need the exact column name and not the lower-case column name. Based on the data showed, the column names are Cylinder
and Doors
and not cylinder
and doors
data
cars <- structure(list(Year = c(2003L, 2006L, 2003L), Car = c("Nissan",
"Nissan", "Honda"), Doors = c(4L, 4L, 2L), Cylinder = c(4L, 4L,
6L), Transmission = c("MT", "MT", "AT")), class = "data.frame",
row.names = c(NA,
-3L))