Error creating a 3-way contingency table with map2

23 Views Asked by At

I want to create a 3-way contingency table with R but I get an error...

Reproducible example

set.seed(42)  
n <- 6
juntar_blocs <- data.frame(id=1:n, 
                           pond_total_index_blocs_iguals = sample(1:n),
                           unitat_especifica = sample(0:1,n, replace = TRUE),
                           responsable = sample(0:1,n, replace = TRUE),
                           actu_transv=  sample(0:1,n, replace = TRUE),
                           util_rec_nuevo_pers = sample(0:1,n, replace = TRUE),
                           ord_aprob = sample(0:1,n, replace = TRUE), 
                           pers_transp_tareas = sample(0:1, n, replace = TRUE),
                           Trams_curt = sample (1:4, n, replace = TRUE))

I have these three lists:


variables_index_subst_ <- c("unitatespecifica", "responsable", "actutransv", "util_rec_nuevo_pers", "ord_aprob", "pers_transp_tareas")

group_var2 <- c("Trams_curt")

compliment_index_blocs <- c("compliment_pond_total_index_blocs_iguals")

I then group them all:

descriptius_trams_nou_index_blocs = expand.grid(variables_index_subst_, group_var2, compliment_index_blocs, stringsAsFactors = F)

I finally write the map function:

map2(descriptius_trams_nou_index_blocs$Var2, descriptius_trams_nou_index_blocs$Var1, descriptius_trams_nou_index_blocs$Var3 ~ round(prop.table(table(juntar_blocs[, .x], juntar_blocs[, .y]), margin=1)*100,1))%>% purrr::set_names(descriptius_nou_index_trams$Var1)

I get this message:

Error in `as_mapper()`:
! Can't convert `.f`, a two-sided formula, to a function.
Backtrace:
 1. ... %>% purrr::set_names(descriptius_nou_index_trams$Var1)
 3. purrr::map2(...)
 4. purrr:::map2_("list", .x, .y, .f, ..., .progress = .progress)
 6. purrr:::as_mapper.default(.f, ...)
 7. rlang::as_function(.f)

Thank you really much!

0

There are 0 best solutions below