[英]Combination of a column based on another one in R
I have not found a question about this precise task, so I'd like to ask how to reach this result in R. I have this dataframe in R. 我没有找到有关此精确任务的问题,因此我想问一下如何在R中达到此结果。我在R中有此数据框。
or<-c("1","1","1","2","2")
pr<-c("a","b","c","w","x")
c<-data.frame(or,pr)
Here the result: 结果如下:
c
or pr
1 1 a
2 1 b
3 1 c
4 2 w
5 2 x
I'd like to have the -not double, ie not aa,bb,cc,xx,ww- combinations of the column pr
, based on the column or
. 我想要基于列
or
的列pr
-not组合,即不是aa,bb,cc,xx,ww-的组合。 The result is something like: 结果是这样的:
d
first second
a b
b a
c b
b c
c a
a c
w x
x w
The first 6 rows are the combinations of the pr
occurrencies with or=1
, and the 7th and 8th row are the combinations of row with or=2
. 前6行是的组合
pr
occurrencies用or=1
,和第七和第八行是行的组合,带or=2
。
I've found the function 我找到了功能
expand.grid(c$pr)
But it does not work, above all it does not put the combinations in the frame of 2 column I need. 但这是行不通的,最重要的是没有将组合放到我需要的2列框架中。
You can use CJ
from data.table
with column or
as the group variable: 您可以将
data.table
CJ
与column or
作为组变量一起使用:
library(data.table)
setDT(c)[, CJ(pr, pr), or][V1 != V2, .(first = V1, second = V2)]
# first second
#1: a b
#2: a c
#3: b a
#4: b c
#5: c a
#6: c b
#7: w x
#8: x w
Update : 更新 :
If you prefer a base R solution, you can use tapply()
with expand.grid()
: 如果您更喜欢基础R解决方案,则可以将
tapply()
与expand.grid()
:
setNames(
do.call(rbind, c(make.row.names = F,
tapply(c$pr, c$or, FUN = function(v) subset(expand.grid(v, v), Var1 != Var2)))),
c('first', 'second'))
# first second
#1 b a
#2 c a
#3 a b
#4 c b
#5 a c
#6 b c
#7 x w
#8 w x
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.