Добро пожаловать в Stack Overflow!
Как насчет этого:
library(nycflights13)
library(conflicted) # useful for dealing with functions in several packages
suppressMessages(conflict_prefer("filter", "dplyr")) # use filter from the dplyr package
suppressPackageStartupMessages(library(tidyverse)) # load tidyverse without messages
data("flights") # from nycflights13
theflights <-
flights %>%
filter(origin == "JFK") %>% # get the filghts from JFK
select (tailnum) # only keep the airplane number
data("planes") # from nycflights13
# if you want the age
inner_join(theflights, planes) %>% # select the records from planes with matching tailnums defined above
filter(year == min(year, na.rm = TRUE)) %>% # get the oldest airplane
select(year) %>% # keep the year
distinct() %>% # remove duplicates
mutate(age = 2013 - year) %>% # calculate the age in 2013
pull(age) # pull age into its own vector
# or
inner_join(theflights, planes) %>%
summarize(theYear = min(year, na.rm = TRUE)) %>%
mutate(age = 2013 - theYear) %>%
pull(age)
# if you want the oldest airplane(s) tail numbers
inner_join(theflights, planes) %>% # select the records from planes with matching tailnums defined above
filter(year == min(year, na.rm = TRUE)) %>% # you can use T or TRUE but TRUE is prefered
select(tailnum) %>%
distinct()
# if you want the oldest airplane(s) model
theModel <-
inner_join(theflights, planes) %>% # select the records from planes with matching tailnums defined above
filter(year == min(year, na.rm = TRUE)) %>% # you can use T or TRUE but TRUE is prefered
select(model) %>%
distinct() %>%
pull()
# get all the airplanes of that model
DC7BFs <-
planes %>%
filter(model == theModel)
Оператор filter()
начинается с поиска наименьшего (т.е. минимального) года после того, как он отбрасывает отсутствующий (т.е. , NA) значения года. Затем он находит все записи, соответствующие этому году.
Теоретически вы можете использовать T или TRUE для logi c проверок. Однако TRUE рекомендуется руководством по стилю R .