将配对传播到组中

时间:2018-08-02 15:00:04

标签: r

我有以下data.frame

df <- data.frame(V1 = c("A","X","A","Z","B","Y"),
           V2 = c("B","Y","C","Y","C","W"),
           stringsAsFactors=FALSE)
df
#   V1 V2
# 1  A  B
# 2  X  Y
# 3  A  C
# 4  Z  Y
# 5  B  C
# 6  Y  W

我想将某个时刻出现的所有值归为一组,并获得以下信息:

list(c("A","B","C"), c("X","Y","Z","W"))
# [[1]]
# [1] "A" "B" "C"
# 
# [[2]]
# [1] "X" "Y" "Z" "W"

1 个答案:

答案 0 :(得分:4)

网络分析可以提供帮助。

library(igraph)
df <- data.frame(V1 = c("A","X","A","Z","B","Y"),
                 V2 = c("B","Y","C","Y","C","W"),
                 stringsAsFactors=FALSE)


g <- graph_from_data_frame(df, directed = FALSE)
clust <- clusters(g)
clusters <- data.frame(name = names(clust$membership), 
                       cluster = clust$membership,
                       row.names = NULL,
                       stringsAsFactors = FALSE)


split(clusters$name, clusters$cluster)
$`1`
[1] "A" "B" "C"

$`2`
[1] "X" "Z" "Y" "W"

您当然可以将所有内容保留在群集data.frame中以进行进一步的分析。