1

Imagine we have a table like this one:

    > my_table <- matrix(c(1,1,2,3,4,5,7,8,9,10,2,3,5,5,5,6,9,10,11,11), ncol=2)
    > names <- c("FROM", "TO")
    > colnames(my_table) <- names
    > my_table
          FROM TO
     [1,]    1  2
     [2,]    1  3
     [3,]    2  5
     [4,]    3  5
     [5,]    4  5
     [6,]    5  6
     [7,]    7  9
     [8,]    8 10
     [9,]    9 11
    [10,]   10 11

This table represents relationships between items, like this:

Case 1
Case 2

My question is, how can the "cases" be distinguished using R?

The result should be:

          FROM TO CASE
     [1,]    1  2    1
     [2,]    1  3    1
     [3,]    2  5    1
     [4,]    3  5    1
     [5,]    4  5    1
     [6,]    5  6    1
     [7,]    7  9    2
     [8,]    8 10    2
     [9,]    9 11    2
    [10,]   10 11    2

Note that each item can only be present in one "case", i.e., item 1 and 7 could not be linked in the example above, if so there would be only a single case:

Not possible in the example above
If so, it would have to be like this

AleG
  • 153
  • 8
  • 1
    Read about igraph package. – zx8754 May 30 '19 at 11:10
  • 1
    Closed as duplicate, let us know if solutions provided in linked post do not work for you. This should work: `library(igraph); g <- graph_from_data_frame(data.frame(my_table)); clusters(g)$membership` – zx8754 May 30 '19 at 11:14
  • 1
    Hi @zx8754, thanks for the help. Indeed that package worked great. `case <-clusters(g)$membership` gave me a double where the column names matched the nodes. So I converted it to a matrix, then to a dataframe and then converted the row names to a column: `case_m <- as.matrix(case); case_df <- as.data.frame(case_m); case_df <- tibble::rownames_to_column(case_df, var="FROM")`. Finally I just had to merge the dataframe to my original table `merge(my_table, case_df, by = "FROM")` gucci! :D – AleG May 30 '19 at 14:34

0 Answers0