0 votes
1 view
in R Programming by (43.2k points)

I am having a data set that is having some information regarding flights and the information is regarding the route details:

Ori.  Dest  Carr. Pass Flights

JFK   LAX   Delta 15004 50

JFK   LAX   JetBl 17434 100

JFK   BOS   Delta 15344 89

ATL   FLR   AmerA 25054 90

OHD   LAX   Delta 19876 95

OHD   LAX   AmerA 12344 45

For output, I only need routes which have only 1 carrier that is something like the following:

JFK   BOS   Delta 15344 89

ATL   FLR   AmerA 25054 90

How to do this in R?

1 Answer

0 votes
by (92.8k points)

For that use the dplyr package and with the help of the package, just group it by the columns that the dataset is having:

library(dplyr)

df %>% group_by(Ori., Dest) %>% filter(n() == 1)

# Ori.  Dest  Carr.  Pass Flights

#  <chr> <chr> <chr> <int>   <int>

#1 JFK   BOS   Delta 15344      89

#2 ATL   FLR   AmerA 25054      90

Then just use the following from the data.table:

library(data.table)

setDT(df)[, .SD[.N == 1], .(Ori., Dest)]

and base R :

subset(df, ave(Flights, Ori., Dest, FUN = length) == 1)

data

df <- structure(list(Ori. = c("JFK", "JFK", "JFK", "ATL", "OHD", "OHD"

), Dest = c("LAX", "LAX", "BOS", "FLR", "LAX", "LAX"), Carr. = c("Delta", 

"JetBl", "Delta", "AmerA", "Delta", "AmerA"), Pass = c(15004L, 

17434L, 15344L, 25054L, 19876L, 12344L), Flights = c(50L, 100L, 

89L, 90L, 95L, 45L)), class = "data.frame", row.names = c(NA, -6L))

If you are wondering what is R programming language, then do check the blog. 

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...