Determine connectivity of data and separate into groups

后端 未结 1 627

I have a simple question on grouping data. Take the data frame:

df <- data.frame(c(1,2,3,5,5),c(2,3,4,7,6))
colnames(df)<- c(\"A\",\"B\")
df
  A B
1 1 2
2          


        
相关标签:
1条回答
  • 2021-01-22 05:25

    You can use a network graph of the data with the igraph package.

    library(igraph)
    g <- graph.data.frame(df)
    plot(g)
    

    From the plot we can see the components of the graph of g and how they relate to each other.

    I don't use this package very often, but it seems like the way to get the components is to use get.vertex.attribute(), as shown below.

    lapply(decompose.graph(g), get.vertex.attribute, "name")
    # [[1]]
    # [1] "1" "2" "3" "4"
    #
    # [[2]]
    # [1] "5" "7" "6"
    
    0 讨论(0)
提交回复
热议问题