R语言 提取与匹配两列的所有可能排列相匹配的观测值

hs1ihplo  于 2022-12-20  发布在  其他
关注(0)|答案(4)|浏览(139)

我有一个包含10,000个观测值的数据框,其中有两个配对数据集。在另一个数据集中,我有几个选定的配对数据集。

test <- data.frame (iso1=c("A", "B", "C"))

data <- data.frame(hosp1=c("A", "B", "C", "D", "E", "C", "A", "B"),
           hosp2=c("B", "c", "F", "C", "G", "A", "H", "A"),
                   dist= c(12,32,23,12,12,45,13))

现在,我想使用测试数据从诸如“A”和“B”、“B”和“A”、“A”和“C”、“C”和“A”、“B”和“C”、“C”和“B”的数据集“数据”中提取形成数据的可能排列的所有观察
我希望得到这样的东西

hosp1  hosp2   dist
A       B       12
B       C       23
C       A       12
B       A       13
ocebsuys

ocebsuys1#

您只需测试test$iso1是否同时出现在hosp1hosp2中:

data[data$hosp1 %in% test$iso1 & data$hosp2 %in% test$iso1, ]
hosp1 hosp2 dist
1     A     B   12
2     B     C   32
6     C     A   45
8     B     A   13

注意,我修正了示例数据中的一个非大写字母和一个缺失的dist值。

ego6inou

ego6inou2#

如果在"hosp"列中找到了"iso1"列中的任何元素,也许我们可以只使用filterif_all来查找

library(dplyr)
data %>% 
  filter(if_all(starts_with('hosp'), ~ .x %in% test$iso1))
  • 输出
hosp1 hosp2 dist
1     A     B   12
2     B     C   23
3     C     A   12
4     B     A   13

或以base R为单位

subset(data, Reduce(`&`, lapply(data[1:2], `%in%`, test$iso1)))
  hosp1 hosp2 dist
1     A     B   12
2     B     C   23
6     C     A   12
8     B     A   13

数据

data <- structure(list(hosp1 = c("A", "B", "C", "D", "E", "C", "A", "B"
), hosp2 = c("B", "C", "F", "C", "G", "A", "H", "A"), dist = c(12, 
23, 32, 12, 45, 12, 10, 13)), class = "data.frame", row.names = c(NA, 
-8L))
oaxa6hgo

oaxa6hgo3#

使用RcppAlgos::permuteGeneral
或者将空排列显示为NA

RcppAlgos::permuteGeneral(test$iso1, 2, FUN=\(x) {
  d <- data[with(data, hosp1 == x[1] & hosp2 == x[2]), 'dist']
  data.frame(hosp=t(x), dist=ifelse(is.null(d), NA_real_, d))
}) |> do.call(what=rbind)
#   hosp.1 hosp.2 dist
# 1      A      B   12
# 2      A      C   NA
# 3      B      A   13
# 4      B      C   32
# 5      C      A   45
# 6      C      B   NA

或者摆脱他们。

RcppAlgos::permuteGeneral(test$iso1, 2, FUN=\(x) {
  d <- data[with(data, hosp1 == x[1] & hosp2 == x[2]), 'dist']
  if (!length(d) == 0) data.frame(hosp=t(x), dist=d) else NULL
}) |> do.call(what=rbind)
#   hosp.1 hosp.2 dist
# 1      A      B   12
# 2      B      A   13
# 3      B      C   32
# 4      C      A   45
  • 数据 *
data <- structure(list(hosp1 = c("A", "B", "C", "D", "E", "C", "A", "B"
), hosp2 = c("B", "C", "F", "C", "G", "A", "H", "A"), dist = c(12, 
32, 23, 12, 12, 45, 13, 13)), class = "data.frame", row.names = c(NA, 
-8L))

test <- structure(list(iso1 = c("A", "B", "C")), class = "data.frame", row.names = c(NA, 
-3L))
sqougxex

sqougxex4#

将一个连接组合与连接的hosp行进行比较。然后询问...在列表中是否为“AB”...

data <- data.frame(hosp1=c("A", "B", "C", "D", "E", "C", "A", "B"),
                   hosp2=c("B", "C", "F", "C", "G", "A", "H", "A"),
                   dist= c(12, 32,  23,   12,   12  ,45,  13, 13)) ### i added a missing value here

combo<-(expand.grid(test$iso1, test$iso1))

data[paste(data$hosp1, data$hosp2) %in% paste(combo$Var1, combo$Var2),]

  hosp1 hosp2 dist
1     A     B   12
2     B     C   32
6     C     A   45
8     B     A   13

相关问题