rcluster-analysisigraphsocial-networking

How to define exact number of communities in a igraph object,?


I'm working on a group of individuals over a year of behavioural observation. The group is subdivided in subgroups. The subgroup compositions are changing over time, thus I created a loop to calculate monthly social network. I'm using the funciton cluster_edge_betweenness to get the subgroups. I get that $membership gives the communities with optimal modularity dy default, but I'd like to limit the number of communities to 2. How could I do that ?

I allready tried many things, from looking into the function cluster_edge_betweenness, or the package Igraph, to looking into other packages. I allready succeded in limiting the number of groups to two in the dendrogramms (nothing difficult), but I can't find a way to exctract the names of two groups in particular.

Here is a piece of made up data that I'm using :

ndata = structure(list(SUBJECT01 = c("Tuhari", "Iena", "Thabo", "Umar", 
"Thabo", "Thabo", "Hillary", "Bulle", "Thabo", "Thabo", "Thabo", 
"Souris", "Souris", "Vincente", "Vincente", "Vincente", "Vincente", 
"Vangha", "Umar", "Umar", "Virgule", "Virgule", "Umar", "Souris", 
"Valrhona", "Uber", "Uber", "Tahira", "Tahira", "Inca", "Priska", 
"Sam", "Inca", "Sam", "Sam", "Iena", "Iena", "Iena", "Georgika", 
"Souris", "Vangha", "Uranus", "Uranus", "Uranus", "Uranus", "Uranus", 
"Uranus", "Uranus", "Vangha", "Souris", "Uhuru", "Uber", "Sam", 
"Pamela", "Tisha", "Inca", "Priska", "Priska", "Priska", "Georgika", 
"Inca", "Inca", "Inca", "Inca", "Inca", "Inca", "Orphee", "Orphee", 
"Umi", "Gindia", "Sam", "Sam", "Umti", "Sam", "Gindia", "Sam", 
"Orphee", "Uhuru", "Sam", "Sam", "Sam", "Sam", "Orphee", "Orphee", 
"Orphee", "Tuhari", "Orphee", "Isidore", "Isidore", "Vangha", 
"Iena", "Berthe", "Veuliah", "Vindoehk", "Umti", "Vindoehk", 
"Souris", "Veuliah", "Uranus", "Georgika", "Umi", "Berthe", "Hillary", 
"Berthe", "Orphee", "Tuhari", "Orphee", "Umar", "Voodoo", "Voodoo", 
"Bulle", "Berthe", "Souris", "Vincente", "Souris", "Pamela", 
"Veuliah", "Umti", "Umi", "Berthe", "Berthe", "Vangha", "Himalaya", 
"Umti", "Umi", "Berthe", "Berthe", "Uber", "Pamela", "Berthe", 
"Berthe", "Uber", "Tahira", "Tuhari", "Virgule", "Orphee", "Himalaya", 
"Himalaya", "Himalaya", "Tahira", "Orphee", "Inca", "Valrhona", 
"Vincente", "Berthe", "Urenna", "Iena", "Umar", "Himalaya", "Virgule", 
"Orphee", "Himalaya", "Himalaya", "Gindia", "Iena", "Orphee", 
"Himalaya", "Tahira", "Priska", "Berthe", "Priska", "Uranus", 
"Uranus", "Urenna", "Sam", "Himalaya", "Himalaya", "Sam", "Umi", 
"Pamela", "Berthe", "Pamela", "Vindoehk", "Berthe", "Veuliah", 
"Uber", "Gindia", "Uranus", "Umti", "Umi", "Berthe", "Berthe", 
"Souris", "Vindoehk", "Gindia", "Berthe", "Berthe", "Berthe", 
"Berthe", "Veuliah", "Urenna", "Isidore", "Berthe", "Berthe", 
"Berthe", "Veuliah", "Veuliah", "Hillary", "Berthe", "Iena", 
"Souris", "Veuliah", "Souris", "Berthe", "Inca", "Georgika", 
"Georgika", "Thabo", "Georgika", "Georgika", "Georgika", "Georgika", 
"Orphee", "Himalaya", "Tuhari", "Umar", "Himalaya", "Urenna", 
"Orphee", "Himalaya", "Himalaya", "Isidore", "Berthe", "Berthe", 
"Vindoehk", "Berthe", "Berthe", "Vindoehk", "Vindoehk", "Vindoehk", 
"Umi", "Hillary", "Berthe", "Berthe", "Hillary", "Umi", "Priska", 
"Berthe", "Berthe", "Uranus", "Uranus", "Voodoo", "Orphee", "Voodoo", 
"Voodoo", "Voodoo", "Berthe", "Veuliah", "Berthe", "Vincente", 
"Berthe", "Berthe", "Berthe", "Berthe", "Vangha", "Vangha", "Sam", 
"Sam", "Sam", "Tuhari", "Himalaya", "Virgule", "Virgule", "Tuhari", 
"Virgule", "Umti", "Berthe", "Uranus", "Berthe", "Berthe", "Tahira", 
"Orphee", "Tahira", "Tahira", "Himalaya", "Orphee", "Tahira", 
"Umar", "Tahira", "Orphee", "Urenna", "Umi", "Gindia", "Inca", 
"Berthe", "Inca", "Sam", "Sam", "Himalaya", "Orphee", "Sam", 
"Berthe", "Berthe", "Uhuru", "Orphee", "Himalaya", "Berthe", 
"Orphee", "Orphee", "Georgika", "Berthe", "Berthe", "Berthe", 
"Berthe", "Sam", "Berthe", "Himalaya", "Orphee", "Orphee", "Sam", 
"Souris", "Hillary", "Uranus", "Berthe", "Berthe", "Virgule", 
"Hillary", "Isidore", "Pamela", "Umi", "Berthe", "Berthe", "Gindia", 
"Berthe", "Virgule", "Hillary", "Priska", "Inca", "Berthe", "Berthe", 
"Berthe", "Berthe", "Berthe", "Berthe", "Iena", "Berthe", "Berthe", 
"Isidore", "Berthe", "Berthe", "Inca", "Berthe", "Georgika", 
"Valrhona", "Berthe", "Berthe", "Vangha", "Urenna", "Uhuru", 
"Himalaya", "Orphee", "Sam", "Himalaya", "Himalaya", "Himalaya", 
"Orphee", "Valrhona", "Gindia", "Valrhona", "Souris", "Inca", 
"Vindoehk", "Vindoehk", "Vangha", "Hillary", "Umi", "Gindia", 
"Gindia", "Uranus", "Tahira", "Voodoo", "Tahira", "Tahira", "Tahira", 
"Voodoo", "Orphee", "Orphee", "Veuliah", "Souris", "Vincente", 
"Souris", "Souris", "Souris", "Souris", "Vincente", "Vincente", 
"Vincente", "Umar", "Tuhari", "Virgule", "Umti", "Tahira", "Tahira", 
"Tahira", "Tahira", "Tahira", "Uhuru", "Thabo", "Vincente", "Thabo", 
"Vincente", "Tuhari", "Tuhari", "Sam", "Sam", "Veuliah", "Veuliah", 
"Orphee", "Berthe", "Tahira", "Tahira", "Virgule", "Tahira", 
"Tahira", "Tahira", "Tahira", "Umar", "Tahira", "Tahira", "Priska", 
"Berthe", "Priska", "Uber", "Isidore", "Bulle", "Umar", "Umar", 
"Sam", "Tahira", "Tahira", "Tahira", "Orphee", "Berthe", "Berthe", 
"Uber", "Hillary", "Berthe", "Uranus", "Berthe", "Uranus", "Berthe", 
"Thabo", "Priska", "Thabo", "Vindoehk", "Iena", "Vindoehk", "Inca", 
"Souris", "Uranus", "Orphee", "Orphee", "Orphee", "Virgule", 
"Priska", "Souris", "Bulle", "Bulle", "Valrhona", "Valrhona", 
"Iena", "Uhuru", "Iena", "Berthe", "Vangha", "Vindoehk", "Veuliah", 
"Hillary", "Hillary", "Uranus", "Gindia", "Gindia", "Voodoo", 
"Thabo", "Bulle", "Thabo", "Valrhona", "Isidore", "Souris", "Souris", 
"Souris", "Vincente", "Veuliah", "Vangha", "Vangha", "Veuliah", 
"Priska", "Umti", "Umti", "Isidore", "Isidore", "Isidore", "Uber", 
"Uber", "Uber", "Uber", "Uber", "Tahira", "Uhuru", "Tahira", 
"Uhuru", "Orphee", "Himalaya", "Sam", "Iena", "Iena", "Uhuru", 
"Uhuru", "Bulle", "Uhuru", "Georgika", "Priska", "Veuliah", "Priska", 
"Urenna", "Urenna", "Sam", "Sam", "Uber", "Uhuru", "Pamela", 
"Gindia", "Uber", "Hillary", "Uhuru", "Berthe", "Berthe", "Isidore", 
"Georgika", "Valrhona", "Inca", "Georgika", "Tuhari", "Souris", 
"Uber", "Isidore", "Souris", "Iena", "Iena", "Souris", "Hillary", 
"Umi", "Gindia", "Uranus", "Orphee", "Thabo", "Thabo", "Thabo", 
"Souris", "Vincente", "Souris", "Gindia", "Berthe", "Virgule", 
"Virgule", "Umti", "Umti", "Uranus", "Uhuru", "Uber", "Uhuru", 
"Tahira", "Vincente", "Inca", "Sam", "Tuhari", "Tuhari", "Tuhari", 
"Uber", "Sam", "Tuhari", "Gindia", "Urenna", "Souris", "Pamela", 
"Uhuru", "Georgika", "Uber", "Uhuru", "Georgika", "Souris", "Orphee", 
"Sam", "Orphee", "Priska", "Vindoehk", "Umi", "Gindia", "Tuhari", 
"Tahira", "Orphee", "Tuhari", "Thabo", "Thabo", "Thabo", "Thabo", 
"Vindoehk", "Souris", "Souris", "Vincente", "Virgule"), SUBJECT02 = c("Virgule", 
"Bulle", "Georgika", "Georgika", "Georgika", "Georgika", "Gindia", 
"Gindia", "Iena", "Iena", "Iena", "Inca", "Inca", "Inca", "Inca", 
"Inca", "Inca", "Isidore", "Orphee", "Orphee", "Orphee", "Orphee", 
"Orphee", "Priska", "Priska", "Priska", "Priska", "Sam", "Sam", 
"Souris", "Souris", "Tahira", "Tahira", "Tahira", "Tahira", "Thabo", 
"Thabo", "Thabo", "Thabo", "Tisha", "Tisha", "Tisha", "Tisha", 
"Tisha", "Tisha", "Tisha", "Tisha", "Tisha", "Tisha", "Tuhari", 
"Uber", "Uhuru", "Umar", "Umti", "Uranus", "Urenna", "Valrhona", 
"Valrhona", "Valrhona", "Veuliah", "Vincente", "Vincente", "Vincente", 
"Vincente", "Vincente", "Vincente", "Virgule", "Virgule", "Gindia", 
"Hillary", "Himalaya", "Himalaya", "Pamela", "Pamela", "Priska", 
"Tahira", "Tahira", "Tahira", "Tahira", "Tahira", "Tahira", "Tahira", 
"Tuhari", "Tuhari", "Umar", "Umar", "Umar", "Umti", "Uranus", 
"Berthe", "Berthe", "Bulle", "Georgika", "Georgika", "Georgika", 
"Georgika", "Georgika", "Georgika", "Gindia", "Gindia", "Gindia", 
"Gindia", "Gindia", "Hillary", "Himalaya", "Himalaya", "Himalaya", 
"Himalaya", "Himalaya", "Himalaya", "Iena", "Iena", "Inca", "Inca", 
"Inca", "Isidore", "Isidore", "Isidore", "Isidore", "Isidore", 
"Isidore", "Isidore", "Orphee", "Pamela", "Pamela", "Pamela", 
"Pamela", "Priska", "Priska", "Priska", "Priska", "Priska", "Sam", 
"Sam", "Sam", "Sam", "Sam", "Sam", "Sam", "Sam", "Sam", "Souris", 
"Souris", "Souris", "Souris", "Souris", "Souris", "Tahira", "Tahira", 
"Tahira", "Tahira", "Tahira", "Tahira", "Thabo", "Thabo", "Tuhari", 
"Tuhari", "Tuhari", "Uber", "Uber", "Uber", "Uber", "Uber", "Uhuru", 
"Uhuru", "Umar", "Umar", "Umar", "Umti", "Umti", "Umti", "Umti", 
"Uranus", "Uranus", "Uranus", "Uranus", "Uranus", "Urenna", "Urenna", 
"Urenna", "Urenna", "Urenna", "Urenna", "Valrhona", "Valrhona", 
"Valrhona", "Valrhona", "Valrhona", "Valrhona", "Vangha", "Vangha", 
"Vangha", "Vangha", "Vangha", "Vangha", "Vangha", "Vangha", "Veuliah", 
"Veuliah", "Veuliah", "Veuliah", "Vincente", "Vincente", "Vincente", 
"Vincente", "Vindoehk", "Vindoehk", "Vindoehk", "Vindoehk", "Vindoehk", 
"Vindoehk", "Vindoehk", "Virgule", "Virgule", "Virgule", "Voodoo", 
"Voodoo", "Voodoo", "Voodoo", "Voodoo", "Voodoo", "Berthe", "Bulle", 
"Bulle", "Georgika", "Georgika", "Georgika", "Georgika", "Georgika", 
"Georgika", "Gindia", "Gindia", "Gindia", "Gindia", "Gindia", 
"Gindia", "Hillary", "Hillary", "Hillary", "Hillary", "Hillary", 
"Himalaya", "Himalaya", "Himalaya", "Himalaya", "Himalaya", "Iena", 
"Iena", "Inca", "Inca", "Isidore", "Isidore", "Isidore", "Isidore", 
"Isidore", "Isidore", "Orphee", "Orphee", "Orphee", "Orphee", 
"Orphee", "Orphee", "Orphee", "Orphee", "Orphee", "Pamela", "Pamela", 
"Priska", "Priska", "Priska", "Sam", "Sam", "Sam", "Sam", "Sam", 
"Sam", "Sam", "Sam", "Sam", "Sam", "Souris", "Souris", "Souris", 
"Souris", "Souris", "Souris", "Tahira", "Tahira", "Tahira", "Tahira", 
"Tahira", "Thabo", "Thabo", "Tuhari", "Tuhari", "Tuhari", "Tuhari", 
"Tuhari", "Tuhari", "Uber", "Uber", "Uber", "Uhuru", "Uhuru", 
"Umar", "Umar", "Umar", "Umar", "Umar", "Umar", "Umi", "Umi", 
"Umi", "Umi", "Umi", "Umi", "Umi", "Umi", "Umti", "Umti", "Umti", 
"Umti", "Uranus", "Uranus", "Uranus", "Uranus", "Urenna", "Urenna", 
"Urenna", "Urenna", "Valrhona", "Valrhona", "Valrhona", "Valrhona", 
"Vangha", "Vangha", "Vangha", "Vangha", "Veuliah", "Veuliah", 
"Vincente", "Vincente", "Vindoehk", "Vindoehk", "Vindoehk", "Vindoehk", 
"Vindoehk", "Virgule", "Virgule", "Virgule", "Virgule", "Virgule", 
"Voodoo", "Voodoo", "Voodoo", "Voodoo", "Berthe", "Berthe", "Berthe", 
"Bulle", "Georgika", "Georgika", "Georgika", "Gindia", "Gindia", 
"Gindia", "Hillary", "Hillary", "Hillary", "Himalaya", "Himalaya", 
"Himalaya", "Himalaya", "Himalaya", "Himalaya", "Iena", "Iena", 
"Iena", "Inca", "Inca", "Inca", "Inca", "Inca", "Inca", "Inca", 
"Inca", "Inca", "Orphee", "Orphee", "Orphee", "Pamela", "Sam", 
"Sam", "Sam", "Sam", "Sam", "Sam", "Souris", "Souris", "Souris", 
"Souris", "Tahira", "Tahira", "Tahira", "Tahira", "Thabo", "Thabo", 
"Thabo", "Thabo", "Tuhari", "Tuhari", "Tuhari", "Tuhari", "Tuhari", 
"Tuhari", "Tuhari", "Tuhari", "Tuhari", "Tuhari", "Uber", "Uber", 
"Uber", "Uhuru", "Uhuru", "Uhuru", "Uhuru", "Uhuru", "Uhuru", 
"Umar", "Umar", "Umar", "Umi", "Umi", "Uranus", "Uranus", "Uranus", 
"Uranus", "Urenna", "Valrhona", "Valrhona", "Valrhona", "Vangha", 
"Vangha", "Veuliah", "Veuliah", "Veuliah", "Vincente", "Vincente", 
"Vincente", "Vindoehk", "Virgule", "Virgule", "Virgule", "Voodoo", 
"Berthe", "Berthe", "Berthe", "Berthe", "Berthe", "Berthe", "Bulle", 
"Bulle", "Bulle", "Bulle", "Georgika", "Georgika", "Georgika", 
"Gindia", "Gindia", "Hillary", "Hillary", "Hillary", "Himalaya", 
"Iena", "Iena", "Iena", "Iena", "Inca", "Inca", "Inca", "Inca", 
"Inca", "Isidore", "Isidore", "Isidore", "Isidore", "Orphee", 
"Pamela", "Pamela", "Priska", "Priska", "Priska", "Priska", "Priska", 
"Priska", "Priska", "Priska", "Sam", "Sam", "Sam", "Sam", "Sam", 
"Tahira", "Tahira", "Thabo", "Thabo", "Thabo", "Thabo", "Thabo", 
"Thabo", "Thabo", "Uber", "Uber", "Uber", "Uhuru", "Uhuru", "Uhuru", 
"Uhuru", "Umti", "Umti", "Umti", "Umti", "Uranus", "Uranus", 
"Urenna", "Valrhona", "Valrhona", "Vangha", "Vangha", "Veuliah", 
"Vincente", "Vindoehk", "Virgule", "Voodoo", "Berthe", "Berthe", 
"Bulle", "Bulle", "Bulle", "Georgika", "Gindia", "Gindia", "Hillary", 
"Hillary", "Himalaya", "Iena", "Iena", "Iena", "Inca", "Inca", 
"Inca", "Isidore", "Isidore", "Orphee", "Orphee", "Pamela", "Pamela", 
"Priska", "Priska", "Priska", "Sam", "Sam", "Souris", "Souris", 
"Tahira", "Uber", "Uber", "Uber", "Umar", "Umar", "Umi", "Umi", 
"Umi", "Umti", "Umti", "Urenna", "Urenna", "Urenna", "Urenna", 
"Urenna", "Vindoehk", "Virgule", "Virgule", "Virgule", "Georgika", 
"Georgika", "Gindia", "Hillary", "Himalaya", "Himalaya", "Himalaya", 
"Himalaya", "Iena", "Iena", "Iena", "Iena", "Inca", "Inca", "Inca", 
"Inca", "Orphee"), DATE = c("2022-07-21", "2022-07-22", "2022-07-22", 
"2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", 
"2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", 
"2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", 
"2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", 
"2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", 
"2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", 
"2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", 
"2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", 
"2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", 
"2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", 
"2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", 
"2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", 
"2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", "2022-07-22", 
"2022-07-23", "2022-07-23", "2022-07-23", "2022-07-23", "2022-07-23", 
"2022-07-23", "2022-07-23", "2022-07-23", "2022-07-23", "2022-07-23", 
"2022-07-23", "2022-07-23", "2022-07-23", "2022-07-23", "2022-07-23", 
"2022-07-23", "2022-07-23", "2022-07-23", "2022-07-23", "2022-07-23", 
"2022-07-23", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", "2022-07-28", 
"2022-07-28", "2022-07-28", "2022-07-28", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", "2022-07-29", 
"2022-07-29", "2022-07-29", "2022-07-29", "2022-08-01", "2022-08-01", 
"2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", 
"2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", 
"2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", 
"2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", 
"2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", 
"2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", 
"2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", 
"2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", 
"2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", 
"2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", 
"2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", 
"2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", 
"2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", 
"2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", 
"2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", 
"2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", 
"2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", 
"2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", 
"2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", "2022-08-01", 
"2022-08-01", "2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", 
"2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", 
"2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", 
"2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", 
"2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", 
"2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", 
"2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", 
"2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", 
"2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", 
"2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", 
"2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", 
"2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", 
"2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", 
"2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", 
"2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", 
"2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", "2022-08-03", 
"2022-08-03", "2022-08-09", "2022-08-09", "2022-08-09", "2022-08-09", 
"2022-08-09", "2022-08-09", "2022-08-09", "2022-08-09", "2022-08-09", 
"2022-08-09", "2022-08-09", "2022-08-09", "2022-08-09", "2022-08-09", 
"2022-08-09", "2022-08-09", "2022-08-09", "2022-08-09", "2022-08-09", 
"2022-08-09", "2022-08-09", "2022-08-09", "2022-08-09", "2022-08-09", 
"2022-08-09", "2022-08-09", "2022-08-09", "2022-08-09", "2022-08-09", 
"2022-08-09", "2022-08-09", "2022-08-09", "2022-08-09", "2022-08-09", 
"2022-08-09", "2022-08-09", "2022-08-09", "2022-08-09", "2022-08-09", 
"2022-08-09", "2022-08-09", "2022-08-09", "2022-08-09", "2022-08-09", 
"2022-08-09", "2022-08-09", "2022-08-09", "2022-08-09", "2022-08-09", 
"2022-08-09", "2022-08-10", "2022-08-10", "2022-08-10", "2022-08-10", 
"2022-08-10", "2022-08-10", "2022-08-10", "2022-08-10", "2022-08-10", 
"2022-08-10", "2022-08-10", "2022-08-10", "2022-08-10", "2022-08-10", 
"2022-08-10", "2022-08-10", "2022-08-10")), row.names = 2400:3000, class = "data.frame")


Here is the code I use for my loop :


  #get the month : 
  ndata$DATE = as.Date(ndata$DATE)
  ndata$MONTH <- format(ndata$DATE, "%y%m")
  ndata$MONTH = as.factor(ndata$MONTH)

  #get the names of subjects
  names = levels(as.factor(ndata$SUBJECT01))

  #get the months
  ww = levels(ndata$MONTH)
  
  #create the newdataframe
  socialnet <- data.frame(Nom=character(),
                 Strength=numeric(), 
                 Degree=numeric(), 
                 Eigenvector=numeric(), 
                 Betweeness=numeric(), 
                 Closeness=numeric(),
                 YM=character(),  
                 stringsAsFactors=FALSE) #computing the datas in there

#The function to get the metrics of social network that I'm interested in : 
metric_calc<-function(net_w){
  t_str<-igraph::strength(net_w)
  t_deg <- igraph::degree(net_w)
  t_eig<-igraph::eigen_centrality(net_w)$vector
  t_bet<-igraph::betweenness(net_w,weight=1/E(net_w)$weight)
  t_clo<-igraph::closeness(net_w,weight=1/E(net_w)$weight)
  output<-data.frame(t_str,t_deg,t_eig,t_bet,t_clo)
  names(output)<-c("Strength","Degree","Eigenvector","Betweenness","Closeness")
  return(output)
}
 
#LOOP to get a new social network for each month and increment the new dataframe with the monthly metrics of social network

for(k in ww){
    wdata = subset(ndata, ndata$MONTH == k)#subset the month
    
    net_month<-graph_from_data_frame(wdata[,1:2],directed=FALSE)
    plot(net_month,vertex.color="lightgray",main = c("social network month", k),edge.color="gray",edge.width=E(net_month)$weight)
    ceb <- cluster_edge_betweenness(net_month)
    dendPlot(ceb, mode="hclust",  main= c("interm?diarit? des liens mois",k), rect=2)
    
    fa_mets<-metric_calc(net_month)#table with all the datas
    fa_mets['YM']= k
    fa_mets <- tibble::rownames_to_column(fa_mets, "Nom")
    fa_mets$group = ceb$membership
    socialnet= rbind(socialnet, fa_mets)
  }

Solution

  • I guess you can use cluster_spinglass to set up number of communities, for example

    ndata %>%
      filter(MONTH == "2207") %>%
      graph_from_data_frame() %>%
      cluster_spinglass(spins = 2) %>%
      membership()
    

    which shows

      Tuhari     Iena    Thabo     Umar  Hillary    Bulle   Souris Vincente 
           1        2        2        1        2        2        2        2
      Vangha  Virgule Valrhona     Uber   Tahira     Inca   Priska      Sam
           2        1        2        2        1        1        2        1
    Georgika   Uranus    Uhuru   Pamela    Tisha   Orphee      Umi   Gindia
           2        2        1        2        2        1        2        2 
        Umti  Isidore   Berthe  Veuliah Vindoehk   Voodoo Himalaya   Urenna
           2        2        2        2        2        1        1        1
    

    and its plot looks like

    g <- ndata %>%
      filter(MONTH == "2207") %>%
      graph_from_data_frame()
    csg <- cluster_spinglass(g, spins = 2)
    

    enter image description here

    If you set spins = 3, the clustering turns to be enter image description here