我有以下数据框:
??????????????????
? Code ? Airline ?
??????????????????
? 1 ? AF ?
? 1 ? KL ?
? 8 ? AR ?
? 8 ? AZ ?
? 8 ? DL ?
??????????????????
dat <- structure(list(Code = c(1L, 1L, 8L, 8L, 8L), Airline = structure(c(1L,
5L, 2L, 3L, 4L), .Label = c("AF ", "AR ", "AZ ", "DL", "KL "
), class = "factor")), .Names = c("Code", "Airline"), class = "data.frame", row.names = c(NA,
-5L))
Run Code Online (Sandbox Code Playgroud)
我的目标是让每家航空公司查找所有共享代码,即一个或多个其他航空公司使用的代码.所以输出就是
+--------------------+
| Airline SharedWith |
+--------------------+
| AF "KL" |
| KL "AF" |
| AR "AZ","DL" |
+--------------------+
Run Code Online (Sandbox Code Playgroud)
伪代码是任何命令式语言
for each code
lookup all rows in the table where the value = code
Run Code Online (Sandbox Code Playgroud)
由于R不是那么多面向列表的,那么实现预期输出的最佳方法是什么?
Jaa*_*aap 10
使用该data.table
包的几个选项:
1)使用strsplit
,paste
与由行操作:
library(data.table)
setDT(dat)[, Airline := trimws(Airline) # this step is needed to remove the leading and trailing whitespaces
][, sharedwith := paste(Airline, collapse = ','), Code
][, sharedwith := paste(unlist(strsplit(sharedwith,','))[!unlist(strsplit(sharedwith,',')) %in% Airline],
collapse = ','), 1:nrow(dat)]
Run Code Online (Sandbox Code Playgroud)
这使:
> dat
Code Airline sharedwith
1: 1 AF KL
2: 1 KL AF
3: 8 AR AZ,DL
4: 8 AZ AR,DL
5: 8 DL AR,AZ
Run Code Online (Sandbox Code Playgroud)
2)使用strsplit
与paste
以mapply
代替by = 1:nrow(dat)
:
setDT(dat)[, Airline := trimws(Airline)
][, sharedwith := paste(Airline, collapse = ','), Code
][, sharedwith := mapply(function(s,a) paste(unlist(strsplit(s,','))[!unlist(strsplit(s,',')) %in% a],
collapse = ','),
sharedwith, Airline)][]
Run Code Online (Sandbox Code Playgroud)
这将给你相同的结果.
3)或者使用CJ
函数paste
(受expand.grid
@ zx8754解决方案的启发):
library(data.table)
setDT(dat)[, Airline := trimws(Airline)
][, CJ(air=Airline, Airline, unique=TRUE)[air!=V2][, .(shared=paste(V2,collapse=',')), air],
Code]
Run Code Online (Sandbox Code Playgroud)
这使:
Code air shared
1: 1 AF KL
2: 1 KL AF
3: 8 AR AZ,DL
4: 8 AZ AR,DL
5: 8 DL AR,AZ
Run Code Online (Sandbox Code Playgroud)
使用dplyr
&tidyr
获得所需解决方案的解决方案(受@jaimedash启发):
library(dplyr)
library(tidyr)
dat <- dat %>% mutate(Airline = trimws(as.character(Airline)))
dat %>%
mutate(SharedWith = Airline) %>%
group_by(Code) %>%
nest(-Code, -Airline, .key = SharedWith) %>%
left_join(dat, ., by = 'Code') %>%
unnest() %>%
filter(Airline != SharedWith) %>%
group_by(Code, Airline) %>%
summarise(SharedWith = toString(SharedWith))
Run Code Online (Sandbox Code Playgroud)
这使:
Code Airline SharedWith
(int) (chr) (chr)
1 1 AF KL
2 1 KL AF
3 8 AR AZ, DL
4 8 AZ AR, DL
5 8 DL AR, AZ
Run Code Online (Sandbox Code Playgroud)
一种igraph
方法
library(igraph)
g <- graph_from_data_frame(dat)
# Find neighbours for select nodes
ne <- setNames(ego(g,2, nodes=as.character(dat$Airline), mindist=2), dat$Airline)
ne
#$`AF `
#+ 1/7 vertex, named:
#[1] KL
#$`KL `
#+ 1/7 vertex, named:
#[1] AF
---
---
# Get final format
data.frame(Airline=names(ne),
Shared=sapply(ne, function(x)
paste(V(g)$name[x], collapse=",")))
# Airline Shared
# 1 AF KL
# 2 KL AF
# 3 AR AZ,DL
# 4 AZ AR,DL
# 5 DL AR,AZ
Run Code Online (Sandbox Code Playgroud)
我想你需要的只是一个 table
dat <- structure(list(Code = c(1L, 1L, 8L, 8L, 8L),Airline = structure(c(1L, 5L, 2L, 3L, 4L),.Label = c("AF", "AR", "AZ", "DL", "KL"),class = "factor")),.Names = c("Code", "Airline"),class = "data.frame", row.names = c(NA, -5L))
tbl <- crossprod(table(dat))
diag(tbl) <- 0
# Airline
# Airline AF AR AZ DL KL
# AF 0 0 0 0 1
# AR 0 0 1 1 0
# AZ 0 1 0 1 0
# DL 0 1 1 0 0
# KL 1 0 0 0 0
dd <- data.frame(Airline = colnames(tbl),
shared = apply(tbl, 1, function(x)
paste(names(x)[x > 0], collapse = ', ')))
merge(dat, dd)
# Airline Code shared
# 1 AF 1 KL
# 2 AR 8 AZ, DL
# 3 AZ 8 AR, DL
# 4 DL 8 AR, AZ
# 5 KL 1 AF
Run Code Online (Sandbox Code Playgroud)
可能会有更有效的路线,但这应该会飞:
# example data
d <- data.frame(code = c(1,1,8,8,8),
airline = c("AF","KL","AR","AZ","DL"),
stringsAsFactors = FALSE)
# merge d to itself on the code column. This isn't necessarily efficient
d2 <- merge(d, d, by = "code")
# prune d2 to remove occasions where
# airline.x and airline.y (from the merge) are equal
d2 <- d2[d2[["airline.x"]] != d2[["airline.y"]], ]
# construct the combinations for each airline using a split, apply, combine
# then, use stack to get a nice structure for merging
d2 <- stack(
lapply(split(d2, d2[["airline.x"]]),
function(ii) paste0(ii$airline.y, collapse = ",")))
# merge d and d2. "ind" is a column produced by stack
merge(d, d2, by.x = "airline", by.y = "ind")
# airline code values
#1 AF 1 KL
#2 AR 8 AZ,DL
#3 AZ 8 AR,DL
#4 DL 8 AR,AZ
#5 KL 1 AF
Run Code Online (Sandbox Code Playgroud)
使用expand.grid和aggregate:
do.call(rbind,
lapply(split(dat, dat$Code), function(i){
x <- expand.grid(i$Airline, i$Airline)
x <- x[ x$Var1 != x$Var2, ]
x <- aggregate(x$Var2, list(x$Var1), paste, collapse = ",")
colnames(x) <- c("Airline", "SharedWith")
cbind(Code = i$Code, x)
}))
# output
# Code Airline SharedWith
# 1.1 1 AF KL
# 1.2 1 KL AF
# 8.1 8 AR AZ,DL
# 8.2 8 AZ AR,DL
# 8.3 8 DL AR,AZ
Run Code Online (Sandbox Code Playgroud)
split
有帮助。这是一个完全可复制的编辑,无需任何附加包即可工作。与 OP data.frame 一起使用 - 在 OP 添加可重现的数据集后更改了它。
# strip white space in Airline names:
dat$Airline <- gsub(" ","",dat$Airline)
li <- split(dat,factor(dat$Code))
do.call("rbind",lapply(li,function(x)
data.frame(Airline = x[1,2],
SharedWith = paste(x$Airline[-1]
,collapse=",")
))
)
Run Code Online (Sandbox Code Playgroud)