将顶点属性分配给网络对象列表

时间:2018-06-08 10:37:30

标签: r list lapply

我有两个数据帧,我可以转换为网络对象,然后将顶点属性添加到这些对象。这将工作如下,我可以绘制它们,看它是否正常工作。

# Dataframe 1

 data_frame_1 = data.frame(event1 = c(1, 2, 1, 0),
             event2 = c(0, 0, 3, 0),
             event3 = c(1, 1, 0, 4),
             row.names = letters[1:4])

 data_matrix_1 <- as.matrix(data_frame_1)

 data_network_1 <- as.network(data_matrix_1, matrix.type='bipartite', loops=FALSE, directed = FALSE, ignore.eval=FALSE, names.eval='weight')

 attribute_1 = data.frame(attribute = c(2, 5, 7, 1), row.names = letters[1:4])

 data_network_1 %v% "attribute_1" <- attribute_1

 plot(data_network_1, vertex.cex="attribute_1")


 # Dataframe 2

data_frame_2 = data.frame(event1 = c(3, 1, 3, 1),
                      event2 = c(2, 1, 0, 3),
                      event3 = c(3, 2, 0, 0),
                      row.names = letters[1:4])

data_matrix_2 <- as.matrix(data_frame_2)

data_network_2 <- as.network(data_matrix_2, matrix.type='bipartite', loops=FALSE, directed = FALSE, ignore.eval=FALSE, names.eval='weight')

attribute_2 = data.frame(attribute = c(3, 1, 2, 9), row.names = letters[1:4])

data_network_2 %v% "attribute_2" <- attribute_2

plot(data_network_2, vertex.cex="attribute_2")

实际上,我有大量的数据帧,我想附加顶点属性。因此,如果我的数据在列表中,我想知道如何做到这一点?

# Data

list_dataframe <-list(data_frame_1, data_frame_2)
list_matrix <- lapply(list_dataframe, as.matrix)
list_network <- lapply (list_matrix, as.network, matrix.type='bipartite', loops=FALSE, directed = FALSE, ignore.eval=FALSE, names.eval='weight')

# Attributes

list_attributes <-list(attribute_1, attribute_2)

将数据放入列表不是问题。但是,我无法弄清楚如何使用lapply执行%v%步骤。

0 个答案:

没有答案