I have the following data frame:
╔══════╦═════════╗
║ Code ║ Airline ║
╠══════╬═════════╣
║ 1 ║ AF ║
║ 1 ║ KL ║
║ 8 ║ AR ║
║ 8 ║ A
Take the following as a comment that is posted as an answer just because this allows more convenient formatting.
for each code
lookup all rows in the table where the value = code
ummm... sorry, I don't get how this psedudocode is related to your desired output
+--------------------+
| Airline SharedWith |
+--------------------+
| AF "KL" |
| KL "AF" |
| AR "AZ","DL" |
+--------------------+
The result of this pseudocode should rather be:
+---------------------+
+ Code + Airlines +
+---------------------+
+ 1 + AF, KL +
+ 2 + AR, AZ, DL +
+---------------------+
That is,
codes <- unique(dat$Code)
data.frame(Code=codes, Airlines = sapply(codes, function(x) paste(subset(dat, Code %in% x)$Airline, collapse=",")))