问题
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)
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),]
# 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
}
}
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.
This is what I expect to get for the class 120-122:
Could someone tell me where I am going wrong? Is there a better way to do this in R? Thanks in advance.
回答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
来源:https://stackoverflow.com/questions/55911683/r-create-unique-combinations-of-ids-in-a-given-class-all-combinations-not-getti