Home > Back-end >  error with the result of a replication procedure
error with the result of a replication procedure

Time:02-11

I have a problem with the following procedure:

library(igraph)
k <- cbind(c(.2,.2),c(.2,0))

sbm_centr <- replicate(
  2000,
  sample_sbm(49, pref.matrix = k, block.sizes = c(24, 25)) %>%
    centr_degree(.)$centralization
)

The issue is that the function generates a result with two arguments (in addition to the ID of each replication), so the procedure reports an error: Error in .$centr_degree(.) : 3 arguments passed to '$' requiring 2"

I tried, unsuccessfully, to save one of the arguments with the following expression: centr_degree(.)$centralization[1])

But the list contains only 0s. Thanks in advance for your comments.

CodePudding user response:

Either block the code with {}

library(dplyr)
library(igraph)
library(purrr)
sbm_centr <- replicate(
  2000,
  sample_sbm(49, pref.matrix = k, block.sizes = c(24, 25)) %>%
    {centr_degree(.)$centralization}
)

or use list element extractions in a pipe with pluck

sbm_centr <- replicate(2000,
   sample_sbm(49, pref.matrix = k, block.sizes = c(24, 25)) %>% 
      centr_degree(.) %>% 
      pluck("centralization"))

-output

> str(sbm_centr)
 num [1:2000] 0.158 0.188 0.128 0.153 0.165 ...

CodePudding user response:

We can use [[ in the pipe like below

sbm_centr <- replicate(
  1,
  sample_sbm(49, pref.matrix = k, block.sizes = c(24, 25)) %>%
    centr_degree(.) %>%
    `[[`("centralization")
)
  • Related