按两列分组并在R中合并级别 [英] Group by two columns and union of levels in R
问题描述
我被困在一个看似微不足道的问题上,但现在无法解决。我什至不知道如何正确地制定它,如果您有任何建议,欢迎您。
我有一个data.frame我想根据两列进行分组/索引。问题是,我要分组的行在那些列中没有共享相同的值。而是,某些行在一个列中具有相同的值,然后其中一些行在第二列中具有与其他行相同的值(我也希望将其包含在分组中)。这是一个最小的示例,我希望这可以使它更清晰一些:
I am stuck on a problem that seem trivial but I am unable to figure it out right now. I don't even know how to formulate it properly, if you have any suggestions, you are welcome. I have a data.frame which I want to group/index depending on two columns. The thing is, the rows I want to group do not share the same values in those columns. Rather, some rows have the same value in one column, and then some of those rows have a common value with different rows in the second column (which I also want to include in the grouping). Here is a minimal example, I hope this makes it a bit clearer:
id V1 V2 group_id
1 a c 1
2 a d 1
3 b d 1
4 w y 2
5 w z 2
6 x z 2
行1和行2共同具有列V1的值 a
。但是我不仅要对它们进行分组,还要对第3行进行分组,该行通过列V2的值 d
被连接。现在,我只能分别对行1,2和2,3进行分组。
Rows 1 and 2 have the value a
of column V1 in common. But I not only want to group them, but also row 3, which is "connected" via the value d
of column V2. Right now, I am only able to group rows 1,2 and 2,3 separately.
第二组也是如此,这里我想对值进行分组在V1中为 w
或在V2中为 z
。 x
和 y
无关。
The same is true for the 2nd group, here I want to group values with either w
in V1 or z
in V2. x
and y
are irrelevant.
任何帮助都是
推荐答案
以下是使用集群
的方法 igraph
包中的函数:
Here's how you could do that with the cluster
function from the igraph
package:
library(igraph)
relations <- data.frame(from=df$V1,to=df$V2)
g <- graph_from_data_frame(relations)
group_id <- data.frame(V=names(clusters(g)$membership),
cluster=clusters(g)$membership,stringsAsFactors=FALSE)
left_join(df,group_id,by=c("V1"="V"))
id V1 V2 group_id cluster
1 1 a c 1 1
2 2 a d 1 1
3 3 b d 1 1
4 4 w y 2 2
5 5 w z 2 2
6 6 x z 2 2
这篇关于按两列分组并在R中合并级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!