R在给定的类中创建ID的唯一组合(未创建所有组合) [英] R create unique combinations of IDs in a given class (all combinations not getting created)

查看:81
本文介绍了R在给定的类中创建ID的唯一组合(未创建所有组合)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似以下的数据集:

Hi I have a dataset like the following:

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位于以下给定的类别中,我试图创建ID的唯一组合: -126。

I am trying to create unique combinations of the IDs if the ID's lie in the given classes: 120-122, 122-124 and 124-126.

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),] 

#编辑-在上一行中包含下限并排除上限

  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的组合。子集操作看起来正确。但是,在组合操作中,有些错误会蔓延至我无法用力的地方。

The output I am getting is weird for some classes. For instance in the class 120-122, I expect to get all the unique combinations for IDs 1,6,9 and 11. However, the combinations I am getting includes player 3 and I also do not get all the combinations for ID 11. Here's the output I get right now. The first part of the image (before the #######) represents the subset of the data for the class 120-122. The part after "########" represents the combinations of the ID's. The subset operation looks correct. However, in the combination operation, some error creeps in which I cannot put my finger on.

这是我期望得到的第120-122类:

This is what I expect to get for the class 120-122:

< img src = https://i.stack.imgur.com/hxton.png alt =在此处输入图片描述>

有人可以告诉我我要去哪里错了?在R中有更好的方法吗?

Could someone tell me where I am going wrong? Is there a better way to do this in R? Thanks in advance.

推荐答案

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

这篇关于R在给定的类中创建ID的唯一组合(未创建所有组合)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆