【问题标题】:R create unique combinations of IDs in a given class (all combinations not getting created)R 在给定的类中创建唯一的 ID 组合(未创建所有组合)
【发布时间】:2019-04-29 22:39:16
【问题描述】:

您好,我有一个如下数据集:

library(gtools)

z <- c(120, 122, 124, 126)
ID <- as.character(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12))
IQ <- c(120.5, 123, 125, 122.5, 122.1, 121.7, 123.2, 123.7, 120.7, 122.3, 120.1, 122)
Section <- c("A", "A", "B", "B", "A", "B", "B", "A", "B", "A", "B", "B")
zz <- data.frame(ID, IQ, Section)

如果ID 位于给定的类中,我正在尝试创建IDs 的唯一组合:120-122、122-124 和 124-126。 p>

combin_list <- list("list",length(z))
Initial_IQ <- 0
jj <- 1

for (IQ1 in z) {
  obs_list <- zz[(zz$IQ < IQ1 & zz$IQ >= Initial_IQ), ] 

  ### Edit - Include the lower bound and exclude the upper bound in the above row

  print("############")
  print(IQ1)
  print(obs_list)
  print("############")
    
  if (nrow(obs_list) > 2) {
    combination_list <- as.data.frame(combinations(n = nrow(obs_list), r = 2, v = obs_list$ID, repeats.allowed = F))
    combination_list$V1 <-  as.character(combination_list$V1) #without this some error creeps up
    combination_list$V2 <- as.character(combination_list$V2)
    combination_list <- combination_list[combination_list$V1 != combination_list$V2, ]
    combination_list <- cbind(combination_list, Previous_IQ_class = Initial_IQ, Next_class = IQ1)

    print(combination_list)
    print("############")
    combin_list[[jj]] <- combination_list
    Initial_IQ <- IQ1
    jj <- jj+1
  }
  else {
    Initial_IQ <- IQ1
    jj <- jj+1
  }
}

对于某些类,我得到的输出很奇怪。例如,在 120-122 类中,我希望获得 ID 1、6、9 和 11 的所有唯一组合。但是,我得到的组合包括玩家 3,我也没有获得 ID 11 的所有组合。这是我现在得到的输出。图像的第一部分(####### 之前)表示 120-122 类的数据子集。 ######## 之后的部分代表 ID 的组合。子集操作看起来是正确的。但是,在组合操作中,出现了一些我无法解决的错误。

这是我对 120-122 班的期望:

谁能告诉我哪里出错了?在 R 中有没有更好的方法来做到这一点?提前致谢。

【问题讨论】:

  • 如果你有 122 个呢?还是124?你能定义如何对边界上的那些进行分组吗?
  • @Onyambu 在每个类中,包括下限并排除上限。例如,在 120-122 类中,包含 120 的观察并排除 122 的 IQ。我还编辑了代码。感谢您指出这一点。
  • 您喜欢任何软件包还是 Base R 最适合您?
  • 我最终需要对大约 1600 万个不同的 z 行执行此操作。可能有不同的 IQ 等级(例如等级大小为 2、3、4 等)。在那种情况下,我以更快的方法为准。但现在,看到问题的有效解决方案会很有帮助。

标签: r data-manipulation


【解决方案1】:
library(tidyverse)
zz%>%
 mutate(ID=as.character(ID),vec=as.character(cut(IQ,c(120,122,124,126),right=F)))%>%
      group_by(vec)%>%
      summarize(if(n()>1)list(data.frame(t(combn(ID,2)),stringsAsFactors = F))
                else list(data.frame(X1=ID,X2=ID,stringsAsFactors = F)))%>%
      unnest()%>%
      bind_cols(read.csv(text=gsub("[^0-9,]","",.$vec),h=F))
# A tibble: 28 x 5
   vec       X1    X2       V1    V2
   <chr>     <chr> <chr> <int> <int>
 1 [120,122) 1     6       120   122
 2 [120,122) 1     9       120   122
 3 [120,122) 1     11      120   122
 4 [120,122) 6     9       120   122
 5 [120,122) 6     11      120   122
 6 [120,122) 9     11      120   122
 7 [122,124) 2     4       122   124
 8 [122,124) 2     5       122   124
 9 [122,124) 2     7       122   124
10 [122,124) 2     8       122   124
# ... with 18 more rows

【讨论】:

  • 你能解释一下发生了什么吗?代码很密集
  • 好久不见。你好吗
  • @akrun 尽我所能,事情并不是那么好。希望你也一切顺利。
  • 继续努力。我相信这对你有用。
  • @Prometheus first gsub("[^0-9,]","",.$vec) 删除您在vec 列中看到的[)。然后使用read.csv 函数将其分成两列。即将此向量作为数据帧读取。然后将其绑定到原始表
猜你喜欢
  • 1970-01-01
  • 2020-10-30
  • 1970-01-01
  • 1970-01-01
  • 2020-01-04
  • 2016-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多