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

subset not working #74

Closed martinamorris closed 3 years ago

martinamorris commented 3 years ago

hey, this is kind of urgent, as it's blocking work for us. we need to subset egor objects by an alter attribute.

your example for subset uses the egor32 data:

data("egor32")
subset(x = egor32, sex == "m", unit = "alter")

which works. Note from the output that the .egoID var in both the ego and alter dfs are both class factor.

But in my case, after building the egor object using a numeric vector for the ego and alter IDs:

ego_main_all <- egor::egor(
    egos = ego_df,
    alters = alter_df %>% filter(ptype %in% 1),
    ID.vars = list(ego = "ego.id", alter="alt.id"),
    ego_design = list(weights = "ego.wawt")
  )

> class(ego_main_all$ego$variables$.egoID)
[1] "character"
> class(ego_main_all$alter$.egoID)
[1] "character"

And when I try to subset by an alter attribute:

>   ego_main <- subset(ego_main_all, keep_active==1, unit="alter")
Error: Can't combine `.egoID` <character> and `.egoID` <double>.
Run `rlang::last_error()` to see where the error occurred.

It also doesn't work if I change the class of the .xxxID vars

> ego_main_all$ego$variables$.egoID <- factor(ego_main_all$ego$variables$.egoID)
> ego_main_all$alter$.egoID <- factor(ego_main_all$alter$.egoID)
> ego_main_all$alter$.altID <- factor(ego_main_all$alter$.altID)
>   ego_main <- subset(ego_main_all, keep_active==1, unit="alter")
Error: Can't combine `.egoID` <factor<70dbf>> and `.egoID` <double>.
Run `rlang::last_error()` to see where the error occurred.

> rlang::last_error()
<error/vctrs_error_incompatible_type>
Can't combine `.egoID` <factor<70dbf>> and `.egoID` <double>.
Backtrace:
  1. base::subset(ego_main_all, keep_active == 1, unit = "alter")
  2. egor:::subset.egor(ego_main_all, keep_active == 1, unit = "alter")
  4. dplyr:::nest_join.data.frame(...)
  5. vctrs::vec_match(x_key, y_split$key)
  7. vctrs::vec_default_ptype2(...)
  8. vctrs::stop_incompatible_type(...)
  9. vctrs:::stop_incompatible(...)
 10. vctrs:::stop_vctrs(...)
Run `rlang::last_trace()` to see the full context.

So I'm stumped

tilltnet commented 3 years ago

I'll investigate this. Your egor object only has ego and alter data, no alter-alter data, right?

In the meantime: Can you please try if the following works to obtain the subset you are looking for?

ego_main_all %>%
    activate(alter) %>%
    filter(keep_active == 1)
tilltnet commented 3 years ago

I was able to replicate this. The issue is caused by egor() creating an empty aatie tibble with numeric variables, when no aaties are specified by the user. The activate() + filter() strategy works regardless.

I'll push a fix for this to github in a few minutes.

@martinamorris Thank you very much for reporting. I recommend using activate() + filter() (as shown in my previous comment) for your data until the next version egor hits CRAN.

Another temporary fix you can employ would be to change the class of ego_main_all$aatie$.egoID to character. Similar to what you tried before for the other .egoIDs.

ego_main_all$aatie$.egoID <- as.character(ego_main_all$aatie$.egoID)
martinamorris commented 3 years ago

Thx!