Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4609958
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:03:47+00:00 2026-05-22T01:03:47+00:00

@Aniko points out that one way to view my problem is that I need

  • 0

@Aniko points out that one way to view my problem is that I need to find the connected components of a graph, where the vertices are called groups and, variables group and nominated_group indicate an edges between those two groups. My goal is to create a variable parent_Group which indexes the connected components. Or as I put it before:

I have a dataframe with four variables: ID, group, and nominated_ID, and nominated_Group.

Consider sister-groups: Groups A and B are sister-groups if there is at least one case in the data where group==A and nominated_group==B, or vice versa.

I would like to create a variable parent_group which takes on a unique value for each set of sister-groups. In other words, no nominations should occur between cases in different parent_groups. Making the parent_group sequential numbers seems like a good idea.

Many thanks for the help I already received here! I can’t really contribute here but note that I try to pay it forward at stats.exchange and on wikipedia.

In my fake data, A and B are sister-groups. Either case ID=4 or ID=5 are sufficient to make this true. Each group is also their own sister-group. The goal, the creation of parent_group, should result in one parent_group for all cases in A or B, and another parent_group for group C

df <- data.frame(ID = c(9, 5, 2, 4, 3, 7), 
  group = c("A", "A", "B", "B", "A", "C"),
  nominated_ID = c(9, 8, 4, 9, 2, 7)     )

df$nominated_group <- with(df, group[match(nominated_ID, ID)])

df

  ID group nominated_ID nominated_group
1  9     A            9               A
2  5     A            8            <NA>
3  2     B            4               B
4  4     B            9               A
5  3     A            2               B
6  7     C            7               C
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:03:47+00:00Added an answer on May 22, 2026 at 1:03 am

    Consider a graph with the groups as its vertices and the edges indicating that the two groups occur for the same ID. Then I think you are looking for connected components of this graph. The following is a quick and dirty (and probably not optimal) implementation of this idea using the graph package:

    library(graph)
    #make some fake data
    nom <- data.frame(group = c("A","A","A","B","B","C","C"),
                      group2 = c("A","A","B","B","A","C","C"),
                stringsAsFactors=FALSE)
    #remove duplicated pairs
    #it will keep A-B distinct from B-A, could probably be fixed
    nom1 <- nom[!duplicated(nom),]
    
    #define empty graph
    grps <- union(unique(nom$group), unique(nom$group2))
    gg <- new("graphNEL", nodes=grps, edgeL=list())
    #add an edge for every pair
    for (i in 1:nrow(nom1)) gg <- addEdge(nom1$group[i], nom1$group2[i], gg, 1)
    
    #find connected components
    cc <- connComp(gg)
    
    #assing parent by matching within cc
    nom$parent <- apply(nom, 1, 
        function(x) which(sapply(cc, function(y) x["group"] %in% y)))
    nom
    
      group group2 parent
    1     A      A      1
    2     A      A      1
    3     A      B      1
    4     B      B      1
    5     B      A      1
    6     C      C      2
    7     C      C      2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For every name in my table I would like to sum up the quantity

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.