Skip to contents

Functions to index and take subsets of egor() objects: manipulate egos, alters, or alter-alter ties.

Usage

# S3 method for egor
subset(x, subset, ..., unit = attr(x, "active"))

# S3 method for egor
[(x, i, j, unit = attr(x, "active"), ...)

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 via ego$, alter$, and aatie$ 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 .srcRow and .tgtRow

contain the index (counting from 1) of the row of the alter being referenced by .srcID and .tgtID. (This can be used to quickly access the attributes of the alters in question.)

...

extra arguments to subset if subset is 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.

Details

Removing or duplicating an ego will also remove or duplicate their alters and ties.

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     66 - 100        82 Poland     45990
#> 2      2 w     18 - 25         24 Australia  11680
#> 3      3 m     66 - 100        83 Poland     36135
#> 4      4 w     66 - 100       100 Poland     34310
#> 5      5 m     56 - 65         60 Australia  32120
#> # ALTER data: 16 × 7
#>   .altID .egoID sex   age      age.years country   income
#> *  <int>  <dbl> <chr> <fct>        <int> <chr>      <dbl>
#> 1      1      1 w     66 - 100        83 USA        59495
#> 2      2      1 m     66 - 100        73 USA         5475
#> 3      1      2 m     36 - 45         44 Australia  12775
#> # ℹ 13 more rows
#> # AATIE data: 10 × 4
#>   .egoID .srcID .tgtID weight
#> *  <int>  <int>  <int>  <dbl>
#> 1      2      1      4  0.333
#> 2      3      1      2  0.667
#> 3      2      3      4  0.333
#> # ℹ 7 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     66 - 100        82 Poland     45990
#> 2      2 w     18 - 25         24 Australia  11680
#> 3      3 m     66 - 100        83 Poland     36135
#> # ALTER data: 10 × 7
#>   .altID .egoID sex   age      age.years country   income
#> *  <int>  <dbl> <chr> <fct>        <int> <chr>      <dbl>
#> 1      1      1 w     66 - 100        83 USA        59495
#> 2      2      1 m     66 - 100        73 USA         5475
#> 3      1      2 m     36 - 45         44 Australia  12775
#> # ℹ 7 more rows
#> # AATIE data: 6 × 4
#>   .egoID .srcID .tgtID weight
#> *  <int>  <int>  <int>  <dbl>
#> 1      2      1      4  0.333
#> 2      3      1      2  0.667
#> 3      2      3      4  0.333
#> # ℹ 3 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     66 - 100        82 Poland     45990
#> 2      3 m     66 - 100        83 Poland     36135
#> 3      5 m     56 - 65         60 Australia  32120
#> # ALTER data: 8 × 7
#>   .altID .egoID sex   age      age.years country   income
#> *  <int>  <dbl> <chr> <fct>        <int> <chr>      <dbl>
#> 1      1      1 w     66 - 100        83 USA        59495
#> 2      2      1 m     66 - 100        73 USA         5475
#> 3      1      3 m     0 - 17          16 Australia  45625
#> # ℹ 5 more rows
#> # AATIE data: 2 × 4
#>   .egoID .srcID .tgtID weight
#> *  <int>  <int>  <int>  <dbl>
#> 1      3      1      2  0.667
#> 2      3      2      4  0.333

    # 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 = "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: 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 = "ego")
#> # EGO data with survey design (active): 10 × 6
#>   .egoID sex   age      age.years country income
#> *  <dbl> <fct> <fct>        <int> <chr>    <dbl>
#> 1      4 w     18 - 25         21 Poland   29565
#> 2      8 w     66 - 100       100 Poland   35040
#> 3     16 w     66 - 100        87 Poland   18980
#> 4     17 w     46 - 55         53 Poland   16790
#> 5     19 w     66 - 100        98 Poland    4015
#> # ℹ 5 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