Functions to index and take subsets of egor() objects: manipulate
egos, alters, or alter-alter ties.
Arguments
- x
an
egor()object.- subset
either an expression evaluated on each of the rows of the selected unit (as in the eponymous argument of
subset()) or a function whose first argument is a row, specifying which egos, alters, or alter-alter ties to keep. The expressions can access variables in the calling environment; columns of the active unit, columns of other units with which the active unit shares an ego viaego$,alter$, andaatie$as well as the following "virtual" columns to simplify indexing:- Ego index
.egoRow contains the index (counting from 1) of the row being evaluated. (This can be used to access vector variables in the calling environment.)
- Alter index
.altRow contains the index (counting from 1) of the row number in the alter table.
- Alter–alter indices
.srcRowand.tgtRow contain the index (counting from 1) of the row of the alter being referenced by
.srcIDand.tgtID. (This can be used to quickly access the attributes of the alters in question.)
- Ego index
- ...
extra arguments to
subsetifsubsetis a function; otherwise unused.- unit
a selector of the unit of analysis being affected: the egos, the alters or the (alter-alter) ties. Note that only one type of unit can be affected at a time. Defaults to the current active unit selected by
activate.egor().- i
numeric or logical vector indexing the appropriate unit.
- j
either an integer vector specifying which columns of the filtered structure (ego, alters, or ties) to select, or a logical vector specifying which columns to keep. Note that the special columns .egoID, .altID, .srcID, .tgtID are not indexed by
j.
Value
An egor() object.
Examples
# Generate a small sample dataset
(e <- make_egor(5,4))
#> # EGO data (active): 5 × 6
#> .egoID sex age age.years country income
#> * <dbl> <chr> <fct> <int> <chr> <dbl>
#> 1 1 m 46 - 55 52 USA 54750
#> 2 2 m 66 - 100 86 Germany 27010
#> 3 3 m 56 - 65 56 Poland 52925
#> 4 4 w 66 - 100 99 Germany 20075
#> 5 5 m 66 - 100 82 Poland 19710
#> # ALTER data: 15 × 7
#> .altID .egoID sex age age.years country income
#> * <int> <dbl> <chr> <fct> <int> <chr> <dbl>
#> 1 1 1 m 36 - 45 36 USA 61320
#> 2 2 1 m 26 - 35 32 Australia 64240
#> 3 3 1 m 0 - 17 4 USA 45625
#> # ℹ 12 more rows
#> # AATIE data: 8 × 4
#> .egoID .srcID .tgtID weight
#> * <int> <int> <int> <dbl>
#> 1 2 1 3 1
#> 2 3 1 2 0.333
#> 3 2 2 3 1
#> # ℹ 5 more rows
# First three egos in the dataset
e[1:3,]
#> # EGO data (active): 3 × 6
#> .egoID sex age age.years country income
#> * <dbl> <chr> <fct> <int> <chr> <dbl>
#> 1 1 m 46 - 55 52 USA 54750
#> 2 2 m 66 - 100 86 Germany 27010
#> 3 3 m 56 - 65 56 Poland 52925
#> # ALTER data: 9 × 7
#> .altID .egoID sex age age.years country income
#> * <int> <dbl> <chr> <fct> <int> <chr> <dbl>
#> 1 1 1 m 36 - 45 36 USA 61320
#> 2 2 1 m 26 - 35 32 Australia 64240
#> 3 3 1 m 0 - 17 4 USA 45625
#> # ℹ 6 more rows
#> # AATIE data: 5 × 4
#> .egoID .srcID .tgtID weight
#> * <int> <int> <int> <dbl>
#> 1 2 1 3 1
#> 2 3 1 2 0.333
#> 3 2 2 3 1
#> # ℹ 2 more rows
# Using an external vector
# (though normally, we would use e[.keep,] here)
.keep <- rep(c(TRUE, FALSE), length.out=nrow(e$ego))
subset(e, .keep)
#> # EGO data (active): 3 × 6
#> .egoID sex age age.years country income
#> * <dbl> <chr> <fct> <int> <chr> <dbl>
#> 1 1 m 46 - 55 52 USA 54750
#> 2 3 m 56 - 65 56 Poland 52925
#> 3 5 m 66 - 100 82 Poland 19710
#> # ALTER data: 8 × 7
#> .altID .egoID sex age age.years country income
#> * <int> <dbl> <chr> <fct> <int> <chr> <dbl>
#> 1 1 1 m 36 - 45 36 USA 61320
#> 2 2 1 m 26 - 35 32 Australia 64240
#> 3 3 1 m 0 - 17 4 USA 45625
#> # ℹ 5 more rows
#> # AATIE data: 4 × 4
#> .egoID .srcID .tgtID weight
#> * <int> <int> <int> <dbl>
#> 1 3 1 2 0.333
#> 2 5 1 2 0.667
#> 3 1 2 3 1
#> # ℹ 1 more row
# Filter egos
subset(x = egor32, subset = egor32$ego$variables$sex == "m", unit="ego")
#> # EGO data with survey design (active): 18 × 6
#> .egoID sex age age.years country income
#> * <dbl> <fct> <fct> <int> <chr> <dbl>
#> 1 1 m 56 - 65 63 Australia 29930
#> 2 2 m 26 - 35 33 Germany 17885
#> 3 3 m 66 - 100 74 Germany 20805
#> 4 5 m 0 - 17 9 Germany 15330
#> 5 6 m 0 - 17 6 Australia 23360
#> # ℹ 13 more rows
#> # ALTER data: 216 × 7
#> .altID .egoID sex age age.years country income
#> * <int> <dbl> <fct> <fct> <int> <chr> <dbl>
#> 1 1 1 m 46 - 55 48 USA 45625
#> 2 2 1 m 0 - 17 5 Germany 52925
#> 3 3 1 w 26 - 35 35 Australia 60225
#> # ℹ 213 more rows
#> # AATIE data: 603 × 4
#> .egoID .srcID .tgtID weight
#> * <int> <int> <int> <dbl>
#> 1 20 1 2 0.667
#> 2 9 6 8 0.667
#> 3 31 2 10 0.667
#> # ℹ 600 more rows
subset(x = egor32, sex == "m")
#> # EGO data with survey design (active): 18 × 6
#> .egoID sex age age.years country income
#> * <dbl> <fct> <fct> <int> <chr> <dbl>
#> 1 1 m 56 - 65 63 Australia 29930
#> 2 2 m 26 - 35 33 Germany 17885
#> 3 3 m 66 - 100 74 Germany 20805
#> 4 5 m 0 - 17 9 Germany 15330
#> 5 6 m 0 - 17 6 Australia 23360
#> # ℹ 13 more rows
#> # ALTER data: 216 × 7
#> .altID .egoID sex age age.years country income
#> * <int> <dbl> <fct> <fct> <int> <chr> <dbl>
#> 1 1 1 m 46 - 55 48 USA 45625
#> 2 2 1 m 0 - 17 5 Germany 52925
#> 3 3 1 w 26 - 35 35 Australia 60225
#> # ℹ 213 more rows
#> # AATIE data: 603 × 4
#> .egoID .srcID .tgtID weight
#> * <int> <int> <int> <dbl>
#> 1 20 1 2 0.667
#> 2 9 6 8 0.667
#> 3 31 2 10 0.667
#> # ℹ 600 more rows
# Filter alters
subset(x = egor32, sex == "m", unit = "alter")
#> # EGO data with survey design (active): 32 × 6
#> .egoID sex age age.years country income
#> * <dbl> <fct> <fct> <int> <chr> <dbl>
#> 1 1 m 56 - 65 63 Australia 29930
#> 2 2 m 26 - 35 33 Germany 17885
#> 3 3 m 66 - 100 74 Germany 20805
#> 4 4 w 18 - 25 21 Poland 29565
#> 5 5 m 0 - 17 9 Germany 15330
#> # ℹ 27 more rows
#> # ALTER data: 180 × 7
#> .altID .egoID sex age age.years country income
#> * <int> <dbl> <fct> <fct> <int> <chr> <dbl>
#> 1 1 1 m 46 - 55 48 USA 45625
#> 2 2 1 m 0 - 17 5 Germany 52925
#> 3 5 1 m 66 - 100 97 Australia 45260
#> # ℹ 177 more rows
#> # AATIE data: 226 × 4
#> .egoID .srcID .tgtID weight
#> * <int> <int> <int> <dbl>
#> 1 24 1 12 0.333
#> 2 16 1 5 0.667
#> 3 22 4 9 0.333
#> # ℹ 223 more rows
# Filter aaties
subset(x = egor32, weight != 0, unit = "aatie")
#> # EGO data with survey design (active): 32 × 6
#> .egoID sex age age.years country income
#> * <dbl> <fct> <fct> <int> <chr> <dbl>
#> 1 1 m 56 - 65 63 Australia 29930
#> 2 2 m 26 - 35 33 Germany 17885
#> 3 3 m 66 - 100 74 Germany 20805
#> 4 4 w 18 - 25 21 Poland 29565
#> 5 5 m 0 - 17 9 Germany 15330
#> # ℹ 27 more rows
#> # ALTER data: 384 × 7
#> .altID .egoID sex age age.years country income
#> * <int> <dbl> <fct> <fct> <int> <chr> <dbl>
#> 1 1 1 m 46 - 55 48 USA 45625
#> 2 2 1 m 0 - 17 5 Germany 52925
#> 3 3 1 w 26 - 35 35 Australia 60225
#> # ℹ 381 more rows
#> # AATIE data: 1,056 × 4
#> .egoID .srcID .tgtID weight
#> * <int> <int> <int> <dbl>
#> 1 20 1 2 0.667
#> 2 25 6 10 0.667
#> 3 9 6 8 0.667
#> # ℹ 1,053 more rows
# Filter egos by alter variables (keep only egos that have more than 13 alters)
subset(x = egor32, nrow(alter) > 13, unit = "ego")
#> # EGO data with survey design (active): 0 × 6
#> # ℹ 6 variables: .egoID <dbl>, sex <fct>, age <fct>, age.years <int>,
#> # country <chr>, income <dbl>
#> # ALTER data: 0 × 7
#> # ℹ 7 variables: .altID <int>, .egoID <dbl>, sex <fct>, age <fct>,
#> # age.years <int>, country <chr>, income <dbl>
#> # AATIE data: 0 × 4
#> # ℹ 4 variables: .egoID <int>, .srcID <int>, .tgtID <int>, weight <dbl>
# Filter alters by ego variables (keep only alters that have egos from Poland)
subset(x = egor32, ego$country == "Poland", unit = "alter")
#> # EGO data with survey design (active): 32 × 6
#> .egoID sex age age.years country income
#> * <dbl> <fct> <fct> <int> <chr> <dbl>
#> 1 1 m 56 - 65 63 Australia 29930
#> 2 2 m 26 - 35 33 Germany 17885
#> 3 3 m 66 - 100 74 Germany 20805
#> 4 4 w 18 - 25 21 Poland 29565
#> 5 5 m 0 - 17 9 Germany 15330
#> # ℹ 27 more rows
#> # ALTER data: 120 × 7
#> .altID .egoID sex age age.years country income
#> * <int> <dbl> <fct> <fct> <int> <chr> <dbl>
#> 1 1 4 m 66 - 100 97 Australia 45260
#> 2 2 4 w 26 - 35 29 Germany 8395
#> 3 3 4 m 26 - 35 32 USA 54020
#> # ℹ 117 more rows
#> # AATIE data: 318 × 4
#> .egoID .srcID .tgtID weight
#> * <int> <int> <int> <dbl>
#> 1 8 6 7 0.667
#> 2 32 3 7 0.667
#> 3 16 1 5 0.667
#> # ℹ 315 more rows
# Filter edges by alter variables (keep only edges between alters where `sex == "m"`)
subset(x = egor32, all(alter$sex == "m"), unit = "aatie")
#> # EGO data with survey design (active): 32 × 6
#> .egoID sex age age.years country income
#> * <dbl> <fct> <fct> <int> <chr> <dbl>
#> 1 1 m 56 - 65 63 Australia 29930
#> 2 2 m 26 - 35 33 Germany 17885
#> 3 3 m 66 - 100 74 Germany 20805
#> 4 4 w 18 - 25 21 Poland 29565
#> 5 5 m 0 - 17 9 Germany 15330
#> # ℹ 27 more rows
#> # ALTER data: 384 × 7
#> .altID .egoID sex age age.years country income
#> * <int> <dbl> <fct> <fct> <int> <chr> <dbl>
#> 1 1 1 m 46 - 55 48 USA 45625
#> 2 2 1 m 0 - 17 5 Germany 52925
#> 3 3 1 w 26 - 35 35 Australia 60225
#> # ℹ 381 more rows
#> # AATIE data: 221 × 4
#> .egoID .srcID .tgtID weight
#> * <int> <int> <int> <dbl>
#> 1 24 1 12 0.333
#> 2 16 1 5 0.667
#> 3 22 4 9 0.333
#> # ℹ 218 more rows