mattflor / chorddiag

R interface to D3 chord diagrams
159 stars 44 forks source link

Creating square matrix from data.frame #21

Closed manuelwaltschek closed 6 years ago

manuelwaltschek commented 6 years ago

Hello there, I know this isn't a problem particularly related to chorddiag, but maybe you can implement some function or give me some tips concerning my problem.

I was aggregating data with dplyr::group_by resulting in an output of a 16x18 data.frame object.

qdmatrix = group_by(mobility_data, destination_activity, source_activity) %>% summarise(count = n()) %>% reshape2::acast(formula = destination_activity~source_activity, drop=FALSE, fill=0)

Activities can have names of a predefined set of factors e.g. "home", "work" ... But the aggregation does not lead to a square matrix, because in the data set there was never a source_activity "church", but there was a destination_activity "church".

For chorddiag I need to supply a square matrix,

Error in chorddiag::chorddiag(qdmatrix) : 'data' must be a square matrix.

but I have no clue how to fill in missing rows or columns, is there a function for such a purpose?

Regards

mattflor commented 6 years ago

This may be more convoluted than necessary but it works:

library(dplyr)
library(tidyr)
library(chorddiag)

activities <- c("home", "work", "church")
mobility_data <- tibble(
    source_activity = sample(activities[1:2], size = 50, replace = TRUE),
    destination_activity = sample(activities, size = 50, replace = TRUE)
) %>% 
    print()

activity_grid <- expand.grid(destination_activity = activities,
                             source_activity = activities) %>% 
    mutate_all(as.character) %>% 
    as_tibble() %>% 
    print()

mobility <- mobility_data %>% 
    group_by(destination_activity, source_activity) %>%
    summarise(count = n()) %>% 
    right_join(activity_grid, by = c("destination_activity", "source_activity")) %>% 
    replace_na(list(count = as.integer(0))) %>% 
    print()

qdmatrix <- mobility %>% 
    reshape2::acast(formula = destination_activity~source_activity) %>% 
    print()

chorddiag(qdmatrix)
manuelwaltschek commented 6 years ago

Thank you @mattflor!