Home > front end >  Returning objects with lapply
Returning objects with lapply

Time:08-16

a small question. I'm trying to get the values for the network indicies (fl_wp_mod, fl_wp_den) stored in seperate variables after running the function. I tried this but I'm able to get it. Any idea why? Sorry, I'm new to lapply and R in general.

cluster_modularity = function(graph_object){
fl_wp_ig <- graph_from_incidence_matrix(graph_object) 
fl_wp_cw <- cluster_walktrap(fl_wp_ig) 
fl_wp_mod <- modularity(fl_wp_cw) 
fl_den <- edge_density(fl_wp_ig, loops = FALSE) 
return(c(fl_wp_mod, fl_den)) 
} 
Mod = lapply(fl_wp_n, cluster_modularity[1]) #fl_wp_n is the raw data
Den = lapply(fl_wp_n, cluster_modularity[2]) #Both these lines are giving me errors 

CodePudding user response:

Using sapply is more convenient here.

ans <- sapply(fl_wp_n, cluster_modularity)
Mod <- ans[1, ]
Den <- ans[2, ]
  • Related