简体   繁体   English

迭代列[R]中的唯一值时如何避免for循环

[英]How to avoid for loop when iterating through unique values in a column [R]

Let's assume that we have following toy data: 假设我们有以下玩具数据:

library(tidyverse)
data <- tibble(
  subject = c(1, 1, 1, 2, 2, 2, 2, 3, 3, 3),
  id1 = c("a", "a", "b", "a", "a", "a", "b", "a", "a", "b"),
  id2 = c("b", "c", "c", "b", "c", "d", "c", "b", "c", "c")
)

which represent network relationships for each subject. 代表每个主题的网络关系。 For example, there are three unique subjects in the data and the network for the first subject could be represented as sequence of relations: 例如,数据中有三个唯一的主题,第一个主题的网络可以表示为关系序列:

a -- b, a --c, b -- c

The task is to compute centralities for each network. 任务是计算每个网络的中心性。 Using for loop this is straightforward: 使用for循环很简单:

library(igraph)
# Get unique subjects
subjects_uniq <- unique(data$subject)

# Compute centrality of nodes for each graph
for (i in 1:length(subjects_uniq)) {
  current_data <- data %>% filter(subject == i) %>% select(-subject)
  current_graph <- current_data %>% graph_from_data_frame(directed = FALSE)
  centrality <- eigen_centrality(current_graph)$vector
}

Question: My dataset is huge so I wonder how to avoid explicit for loop. 问题:我的数据集很大,所以我想知道如何避免显式的for循环。 Should I use apply() and its modern cousins (maybe map() in the purrr package)? 我应该使用apply()及其现代表亲(也许在purrr包中使用map() )? Any suggestions are greatly welcome. 任何建议都非常欢迎。

Here is an option using map 这是使用map的选项

library(tidyverse)
library(igraph)
map(subjects_uniq, ~data %>%
                    filter(subject == .x) %>%
                    select(-subject) %>%
                    graph_from_data_frame(directed = FALSE) %>% 
                    {eigen_centrality(.)$vector})
#[[1]]
#a b c 
#1 1 1 

#[[2]]
#        a         b         c         d 
#1.0000000 0.8546377 0.8546377 0.4608111 

#[[3]]
#a b c 
#1 1 1 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM