1

Say for instance my data is:

Year Car      Doors Cylinder Transmission
2003 Nissan   4     4        MT
2006 Nissan   4     4        MT
2003 Honda    2     6        AT 

I want Shiny to return the first instance MT shows. How would I go about that?

My code:

df_example <- cars %>%
    filter(cylinder = 4, 
           doors = 4) %>%
    slice(1)
lmnguyen
  • 15
  • 4

1 Answers1

0

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))
akrun
  • 874,273
  • 37
  • 540
  • 662