tilltnet / egor

R Package for importing and analysing ego-centered-network data.
http://egor.tillt.net
GNU Affero General Public License v3.0
23 stars 4 forks source link

Remove alters and ties from egor object #89

Open PCK1992 opened 3 months ago

PCK1992 commented 3 months ago

Hello,

Perhaps I'm missing something and I checked the documentation but couldn't figure this out yet. Is there any way to remove egos based on some attribute from an egor object after it has been created?

Suppose I create a an egor object using the egor function:

p2p_net <- egor(alters = ego_nets,
           egos = main,
           aaties = alt_pairs,
           ID.vars = list(
             ego = "su_id",
             alter = "person",
             source = "alter1",
             target = "alter2"))

The ego_nets object contains information on the alters and I want to remove all alters from the object if they have attribute1=1 and all other attribute from a set of specified attributes should be zero e.g. attribute2=0, attribute3=0, etc.

My current code looks like this:

p2p_net_filtered <- subset(p2p_net, 
                           subset = !(all(
                             alter$relationship_healthprov == 1 & 
                             rowSums(alter[, c("relationship_spouse", "relationship_partner", "relationship_parent", 
                                               "relationship_sibling", "relationship_child", "relationship_grandparent", 
                                               "relationship_grandchild", "relationship_relative", "relationship_friend", 
                                               "relationship_coworker", "relationship_neighbor", "relationship_roommate", 
                                               "relationship_churchmember", "relationship_other")]) == 0)),
                           unit = "aatie")

This code keeps crashing on my 32GB laptop.

Is this the way to go to remove unwanted alters for my egor object? Also, is there a more efficient way to do this? There are approximately 2000 ego networks in the data set.

I appreciate any help!

tilltnet commented 3 months ago

Hi, thank you so much for filing this issue. Here is an example on how to filter alters in an egor object.

library(egor)

# Create example data
e <- egor(
  alters = tibble(
    egoID = as.numeric(gl(20, 5)),
    alterID = rep(1:5, 20),
    attribute1 = sample(0:1, 100, replace = TRUE),
    attribute2 = sample(0:1, 100, replace = TRUE),
    attribute3 = sample(0:1, 100, replace = TRUE)
  ),

  egos = tibble(egoID = 1:20),
  aaties = tibble(
    egoID = as.numeric(gl(20, 5)),
    Source = sample(1:5, 100, replace = TRUE),
    Target = sample(1:5, 100, replace = TRUE)
  )
)

activate() the alter dataset and then use dplyr::filter() to filter the alters data.

e |>
  activate(alter) |>
  filter(attribute1 == 1)
#> # EGO data: 20 × 1
#>   .egoID
#> *  <int>
#> 1      1
#> 2      2
#> 3      3
#> # ℹ 17 more rows
#> # ALTER data (active): 55 × 5
#>   .altID .egoID attribute1 attribute2 attribute3
#> *  <int>  <dbl>      <int>      <int>      <int>
#> 1      1      1          1          0          0
#> 2      2      1          1          0          0
#> 3      3      1          1          1          1
#> 4      3      2          1          1          0
#> 5      4      2          1          1          1
#> # ℹ 50 more rows
#> # AATIE data: 35 × 3
#>   .egoID .srcID .tgtID
#> *  <dbl>  <int>  <int>
#> 1      1      3      2
#> 2      1      2      2
#> 3      2      4      3
#> # ℹ 32 more rows

To do a filter operation as you describe:

e |>
  activate(alter) |>
  filter(attribute1 == 1 &
           if_all(attribute2:attribute3, \(x) x == 0))
#> # EGO data: 20 × 1
#>   .egoID
#> *  <int>
#> 1      1
#> 2      2
#> 3      3
#> # ℹ 17 more rows
#> # ALTER data (active): 14 × 5
#>   .altID .egoID attribute1 attribute2 attribute3
#> *  <int>  <dbl>      <int>      <int>      <int>
#> 1      1      1          1          0          0
#> 2      2      1          1          0          0
#> 3      2      3          1          0          0
#> 4      2      4          1          0          0
#> 5      4      6          1          0          0
#> # ℹ 9 more rows
#> # AATIE data: 3 × 3
#>   .egoID .srcID .tgtID
#> *  <dbl>  <int>  <int>
#> 1      1      2      2
#> 2     12      1      1
#> 3     14      1      2