在字符向量中查找唯一的条目配对 [英] Find unique pairings of entries in a character vector
问题描述
我有一个向量 fruit
,其中包含三个条目 Peach、Plum、Pear
.我想在 fruit
中找到每个唯一的配对并创建一个新的两列 data.frame(例如下面的 df.new).对于更大的数据集,我如何在 r 中执行此操作?expand.grid
导致 pear-plum
和 plum-pear
不是唯一的配对,或者不是我正在寻找的配对.有什么建议吗?
I have a vector fruit
with three entries Peach, Plum, Pear
. I would like to find each unique pairing in fruit
and create a new, two column data.frame (e.g. df.new below). How might I do this in r for an even larger data.set? expand.grid
results in pear-plum
and plum-pear
which are not unique pairings, or not the ones I am seeking. Any suggestions?
fruit <- c("Peach", "Plum", "Pear")
fruit1 <- c("Peach", "Peach", "Plum")
fruit2 <- c("Plum", "Pear", "Pear")
df.new <- data.frame(fruit1, fruit2)
#df.new
fruit1 fruit2
1 Peach Plum
2 Peach Pear
3 Plum Pear
# attempt
fruit.y <- fruit
df.expand <- expand.grid(fruit,fruit.y)
推荐答案
使用你的初始策略,你仍然可以尝试扩展网格:
Using your initial strategy, you can still try expand grid:
fruit_df <- expand.grid(fruit,fruit)
然后按水果对每一行进行排序并删除重复项:
Then sort each row by fruit and delete the duplicates:
fruit_df2 <- as.data.frame(unique(t(apply(fruit_df, 1, function(x) sort(x))))
V1 V2
1 Peach Peach
2 Peach Plum
3 Peach Pear
4 Plum Plum
5 Pear Plum
6 Pear Pear
另一种策略是在fruit
中生成所有对的组合,尝试:
Another strategy is to generate all combination of pairs in fruit
, try:
combn(fruit,2)
[,1] [,2] [,3]
[1,] "Peach" "Peach" "Plum"
[2,] "Plum" "Pear" "Pear"
或者将您的输出作为数据框,转置结果并重铸:
Or to make your output as a data frame, transpose the results and recast:
as.data.frame(t(combn(fruit,2)))
注意,使用 combn
你不会得到 plum-plum
.
Note that using combn
you will not get the plum-plum
.
这篇关于在字符向量中查找唯一的条目配对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!