Home > OS >  Subset a dataframe nested inside a list
Subset a dataframe nested inside a list

Time:09-18

I have the list with nested objects below and inside this list there is a nested data frame named edges.

A tibble: 2 × 5
  from                        to                          from_id to_id value
  <chr>                       <chr>                         <int> <int> <dbl>
1 ARTIFICIAL_START            Forged Wire, Medium (Sport)       2     3     0
2 Forged Wire, Medium (Sport) ARTIFICIAL_END                    3     1     0

I wonder how could I approach and modify it by subseting it based on the 2 columns from and to. For example when from is ARTIFICIAL_START and to is Forged Wire, Medium (Sport) then this row is totally deleted. I want to subset it while its inside the list or extract it,subset it and then insert it inside the list again.

perf<-structure(list(x = list(diagram = "digraph {\n\ngraph [layout = \"dot\",\n       outputorder = \"edgesfirst\",\n       bgcolor = \"white\",\n       rankdir = \"LR\"]\n\nnode [fontname = \"Helvetica\",\n      fontsize = \"10\",\n      shape = \"circle\",\n      fixedsize = \"true\",\n      width = \"0.5\",\n      style = \"filled\",\n      fillcolor = \"aliceblue\",\n      color = \"gray70\",\n      fontcolor = \"gray50\"]\n\nedge [fontname = \"Helvetica\",\n     fontsize = \"8\",\n     weight = \"1.5\",\n     color = \"gray80\",\n     arrowsize = \"0.5\"]\n\n  \"1\" [label = \"End\", shape = \"circle\", style = \"rounded,filled\", fontcolor = \"brown4\", color = \"brown4\", tooltip = \"ARTIFICIAL_END\n2\", penwidth = \"1.5\", fixedsize = \"FALSE\", fontname = \"Arial\", fontsize = \"10\", fillcolor = \"#FFFFFF\"] \n  \"2\" [label = \"Start\", shape = \"circle\", style = \"rounded,filled\", fontcolor = \"chartreuse4\", color = \"chartreuse4\", tooltip = \"ARTIFICIAL_START\n2\", penwidth = \"1.5\", fixedsize = \"FALSE\", fontname = \"Arial\", fontsize = \"10\", fillcolor = \"#FFFFFF\"] \n  \"3\" [label = \"Forged Wire, Medium (Sport)\n2\", shape = \"rectangle\", style = \"rounded,filled\", fontcolor = \"black\", color = \"grey\", tooltip = \"Forged Wire, Medium (Sport)\n2\", penwidth = \"1.5\", fixedsize = \"FALSE\", fontname = \"Arial\", fontsize = \"10\", fillcolor = \"#74A9CF\"] \n\"2\"->\"3\" [label = \"2\", penwidth = \"5\", color = \"dodgerblue4\", fontname = \"Arial\", fontsize = \"10\", weight = \"1\", constraint = \"TRUE\"] \n\"3\"->\"1\" [label = \"2\", penwidth = \"5\", color = \"dodgerblue4\", fontname = \"Arial\", fontsize = \"10\", weight = \"1\", constraint = \"TRUE\"] \n}", 
    config = list(engine = "dot", options = NULL)), width = NULL, 
    height = NULL, sizingPolicy = list(defaultWidth = NULL, defaultHeight = NULL, 
        padding = NULL, viewer = list(defaultWidth = NULL, defaultHeight = NULL, 
            padding = NULL, fill = TRUE, suppress = FALSE, paneHeight = NULL), 
        browser = list(defaultWidth = NULL, defaultHeight = NULL, 
            padding = NULL, fill = FALSE, external = FALSE), 
        knitr = list(defaultWidth = NULL, defaultHeight = NULL, 
            figure = TRUE)), dependencies = NULL, elementId = NULL, 
    preRenderHook = NULL, jsHooks = list()), class = c("grViz", 
"htmlwidget"), package = "DiagrammeR", base_precedence = structure(list(
    next_act = c(NA, NA, "ARTIFICIAL_END", "ARTIFICIAL_END", 
    "ARTIFICIAL_START", "Forged Wire, Medium (Sport)", "Forged Wire, Medium (Sport)"
    ), act = c("ARTIFICIAL_END", "ARTIFICIAL_END", "Forged Wire, Medium (Sport)","Forged Wire, Medium (Sport)", NA, "ARTIFICIAL_START", "ARTIFICIAL_START"
    ), aid = c("WC4120721-CN354877", "WC4120667-CN354878", "WC4120721-CN354877", 
    "WC4120667-CN354878", NA, "WC4120721-CN354877", "WC4120667-CN354878"
    ), case = c("WC4120721", "WC4120667", "WC4120721", "WC4120667", 
    NA, "WC4120721", "WC4120667"), start_time = structure(c(1606964400, 
    1607115480, 1606964400, 1607115480, NA, 1606964400, 1607115480
    ), tzone = "", class = c("POSIXct", "POSIXt")), end_time = structure(c(1606964400, 
    1607115480, 1606964400, 1607115480, NA, 1606964400, 1607115480
    ), tzone = "", class = c("POSIXct", "POSIXt")), min_order = c(Inf, 
    Inf, 1, 2, NA, -Inf, -Inf), next_start_time = structure(c(NA, 
    NA, 1606964400, 1607115480, NA, 1606964400, 1607115480), tzone = "", class = c("POSIXct", 
    "POSIXt")), next_end_time = structure(c(NA, NA, 1606964400, 
    1607115480, NA, 1606964400, 1607115480), tzone = "", class = c("POSIXct", 
    "POSIXt")), from_id = c(1L, 1L, 3L, 3L, NA, 2L, 2L), to_id = c(NA, 
    NA, 1L, 1L, 2L, 3L, 3L)), row.names = c(NA, -7L), class = "data.frame"), edges = structure(list(
    from = c("ARTIFICIAL_START", "Forged Wire, Medium (Sport)"
    ), to = c("Forged Wire, Medium (Sport)", "ARTIFICIAL_END"
    ), from_id = 2:3, to_id = c(3L, 1L), value = c(2, 2)), row.names = c(NA, 
-2L), class = c("tbl_df", "tbl", "data.frame")), nodes = structure(list(
    node = c("ARTIFICIAL_END", "ARTIFICIAL_START", "Forged Wire, Medium (Sport)"
    ), from_id = 1:3, value = c(2, 2, 2)), row.names = c(NA, 
-3L), class = c("tbl_df", "tbl", "data.frame"), na.action = structure(c(`4` = 4L), class = "omit")))

CodePudding user response:

It's stored in the attribute "edges" which you may manipulate in a function similar to this one.

f <- \(data, nofrom, noto) {
  u <- attr(data, 'edges')
  `attr<-`(data, 'edges', u[u$from != nofrom & u$to != noto,,drop=FALSE])
}

f(data=perf, nofrom='ARTIFICIAL_START', noto='Forged Wire, Medium (Sport)')
# ...
# attr(,"edges")
# # A tibble: 1 × 5
#     from                        to             from_id to_id value
#     <chr>                       <chr>            <int> <int> <dbl>
#   1 Forged Wire, Medium (Sport) ARTIFICIAL_END       3     1     2
# attr(,"nodes")
# ...
  • Related