More prosaically, create a new list-column where each element is
calculated as some function of the corresponding row. So you would first
create a function that takes a shelf (see rack_extract_shelf()) and
outputs some value, most often some summary dataframe for the model. And
then you calculate summaries for all the models of a rack with the help of
rack_put() function.
Examples
# extract one shelf to develop your function
shelf <- rack_extract_shelf(example_seg_rack, 1)
# this will return a vector of relabelled segments such that
# segment #1 is always the largest one, #2 is the second largerst and so on
library(dplyr)
#>
#> Attaching package: ‘dplyr’
#> The following object is masked from ‘package:Biobase’:
#>
#> combine
#> The following objects are masked from ‘package:BiocGenerics’:
#>
#> combine, intersect, setdiff, union
#> The following objects are masked from ‘package:stats’:
#>
#> filter, lag
#> The following objects are masked from ‘package:base’:
#>
#> intersect, setdiff, setequal, union
shelf_make_reordered_segments <- function(shelf) {
new_levels <-
tibble(
weight = shelf$data$weight,
segment = shelf$segments
) %>%
group_by(segment) %>%
summarise(across(weight, sum)) %>%
arrange(desc(weight)) %>%
mutate(segment_new = row_number()) %>%
arrange(segment) %>%
pull(segment_new)
res <- shelf$segments
# rename levels
levels(res) <- new_levels
# put levels in 1, 2, 3, ... order
res <- factor(res, sort(new_levels))
res
}
# test
shelf_make_reordered_segments(shelf)
#> [1] 2 2 2 1 2 2 1 2 2 2 1 1 1 3 1 1 3 2 1 1 1 3 3 1 1 2 1 1 1 2 1 1 3 3 3 3 2
#> [38] 2 2 1 2 1 3 2 1 3 1 2 3 3 2 1 2 1 1 3 1 2 1 3 3 3 1 1 3 2 1 3 1 2 1 1 1 1
#> [75] 3 1 2 1 1 1 1 3 1 1 1 2 1 1 1 1 3 1 2 1 1 3 2 1 2 2
#> Levels: 1 2 3
# apply this function to the rack
new_rack <- rack_put(
example_seg_rack,
shelf_make_reordered_segments,
"reordered_segments"
)
new_rack$reordered_segments
#> [[1]]
#> [1] 2 2 2 1 2 2 1 2 2 2 1 1 1 3 1 1 3 2 1 1 1 3 3 1 1 2 1 1 1 2 1 1 3 3 3 3 2
#> [38] 2 2 1 2 1 3 2 1 3 1 2 3 3 2 1 2 1 1 3 1 2 1 3 3 3 1 1 3 2 1 3 1 2 1 1 1 1
#> [75] 3 1 2 1 1 1 1 3 1 1 1 2 1 1 1 1 3 1 2 1 1 3 2 1 2 2
#> Levels: 1 2 3
#>
#> [[2]]
#> [1] 3 2 3 1 3 3 1 2 3 2 4 1 1 2 1 1 2 2 1 1 1 1 4 1 1 3 3 1 1 3 1 1 4 4 4 2 3
#> [38] 2 2 1 3 1 2 3 1 2 4 3 2 4 2 1 3 3 4 2 1 3 4 1 2 4 1 1 3 2 4 2 1 3 1 1 4 1
#> [75] 2 1 2 1 1 1 1 4 4 4 1 4 1 1 1 1 4 1 2 1 4 4 3 1 3 3
#> Levels: 1 2 3 4
#>
#> [[3]]
#> [1] 2 3 3 1 3 4 1 4 3 3 5 1 1 2 1 1 2 3 4 1 1 2 2 4 2 4 1 1 1 1 1 1 2 2 2 2 3
#> [38] 3 3 1 4 5 2 5 2 2 2 4 3 5 3 4 3 4 5 2 2 2 1 2 3 2 2 1 1 3 2 2 1 4 1 2 5 4
#> [75] 2 4 3 4 1 4 1 2 5 2 1 5 1 1 4 1 5 1 3 1 5 2 5 1 3 1
#> Levels: 1 2 3 4 5
#>