2017-10-16 1 views
-7

를 사용하여 루프를 다시 작성 :R :이 루프를 가지고 sapply

for(i in E(g)){ 
    a = ends(g, i)[1] 
    b = ends(g, i)[2] 
    source_neighbors = neighbors(g, a) 
    target_neighbors = neighbors(g, b) 
    num_overlap_neighbors = length(intersection(source_neighbors, target_neighbors)) 
    print(num_overlap_neighbors) 
} 

g, 나는 패키지를 igraph 사용 된 GML 그래프이다. 이 함수를 함수로 다시 쓰고 싶으면 sapply()을 사용하여 E(g)에 함수를 적용하여 벡터를 출력으로 가져옵니다.

+0

이 사이트에는'lapply' /'sapply' 루프를 작성하는 방법에 대한 수많은 예제가 있습니다. 우리는 정말로 다른 질문/(자기 답) 쌍이 필요하지 않습니다. – Roland

답변

-2
results=sapply(E(g), function(i){ 
    a = ends(g, i)[1] 
    b = ends(g, i)[2] 
    source_neighbors = neighbors(g, a) 
    target_neighbors = neighbors(g, b) 
    num_overlap_neighbors = length(intersection(source_neighbors, target_neighbors)) 

    return(num_overlap_neighbors) 
}) 
print(results) 
+0

자세히 설명해주세요. – OmG