View source: R/sna-operators.R
gapply | R Documentation |
Returns a vector or array or list of values obtained by applying a function to vertex neighborhoods of a given order.
gapply(X, MARGIN, STATS, FUN, ..., mode = "digraph", diag = FALSE,
distance = 1, thresh = 0, simplify = TRUE)
X |
one or more input graphs. |
MARGIN |
a vector giving the “margin” of |
STATS |
the vector or matrix of vertex statistics to be used. |
FUN |
the function to be applied. In the case of operators, the function name must be quoted. |
... |
additional arguments to |
mode |
|
diag |
boolean; are the diagonals of |
distance |
the maximum geodesic distance at which neighborhoods are to be taken. 1 signifies first-order neighborhoods, 2 signifies second-order neighborhoods, etc. |
thresh |
the threshold to be used in dichotomizing |
simplify |
boolean; should we attempt to coerce output to a vector if possible? |
For each vertex in X
, gapply
first identifies all members of the relevant neighborhood (as determined by MARGIN
and distance
) and pulls the rows of STATS
associated with each. FUN
is then applied to this collection of values. This provides a very quick and easy way to answer questions like:
How many persons are in each ego's 3rd-order neighborhood?
What fraction of each ego's alters are female?
What is the mean income for each ego's trading partners?
etc.
With clever use of FUN
and STATS
, a wide range of functionality can be obtained.
The result of the iterated application of FUN
to each vertex neighborhood's STATS
.
Carter T. Butts buttsc@uci.edu
apply
, sapply
#Generate a random graph
g<-rgraph(6)
#Calculate the degree of g using gapply
all(gapply(g,1,rep(1,6),sum)==degree(g,cmode="outdegree"))
all(gapply(g,2,rep(1,6),sum)==degree(g,cmode="indegree"))
all(gapply(g,c(1,2),rep(1,6),sum)==degree(symmetrize(g),cmode="freeman")/2)
#Find first and second order neighborhood means on some variable
gapply(g,c(1,2),1:6,mean)
gapply(g,c(1,2),1:6,mean,distance=2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.