I have a data.frame
that is grouped by folder
, z_stack_id
and contains the counts for each binary_layer
. The "primary" layers are FITC, TRITC, and Cy5
. I have already calculated intersections elsewhere (). My goal is to calculate the proportions within z_stack, within folder (and other grouping variable if in case needed). I was hoping to use dplyr::group_by(...) %>% summarise(my_custom_fancy_function)
. But I am uncertain how to go about making such function.
The expected outputs for the function would be the proportions for each main layer, grouped by folder/z_stack_id/(...). For example, for Cy5
FITC_Cy5/Cy5
, TRITC_Cy5/Cy5
, Triple/Cy5
Notice that Triple
does not always have counts, so I would need to fill the groups first (currently working on it).
my_df
# A tibble: 13 x 4
folder z_stack_id binary_layer n_blobs
<chr> <dbl> <chr> <int>
1 20180601_122650_896 1.00 Cy5 959
2 20180601_122650_896 1.00 FITC 16
3 20180601_122650_896 1.00 TRITC 499
4 20180601_122650_896 2.00 Cy5 225
5 20180601_122650_896 2.00 FITC 157
6 20180601_122650_896 2.00 TRITC 19
7 20180601_122650_896 1.00 FITC_Cy5 5
8 20180601_122650_896 1.00 FITC_TRITC 2
9 20180601_122650_896 1.00 TRITC_Cy5 301
10 20180601_122650_896 2.00 FITC_Cy5 34
11 20180601_122650_896 2.00 FITC_TRITC 8
12 20180601_122650_896 2.00 Triple 4
13 20180601_122650_896 2.00 TRITC_Cy5 8
dput(my_df)
structure(list(folder = c("20180601_122650_896", "20180601_122650_896",
"20180601_122650_896", "20180601_122650_896", "20180601_122650_896",
"20180601_122650_896", "20180601_122650_896", "20180601_122650_896",
"20180601_122650_896", "20180601_122650_896", "20180601_122650_896",
"20180601_122650_896", "20180601_122650_896"), z_stack_id = c(1,
1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 2), binary_layer = c("Cy5",
"FITC", "TRITC", "Cy5", "FITC", "TRITC", "FITC_Cy5", "FITC_TRITC",
"TRITC_Cy5", "FITC_Cy5", "FITC_TRITC", "Triple", "TRITC_Cy5"),
n_blobs = c(959L, 16L, 499L, 225L, 157L, 19L, 5L, 2L, 301L,
34L, 8L, 4L, 8L)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -13L), .Names = c("folder", "z_stack_id",
"binary_layer", "n_blobs"))
UPDATE
I made an example of the Cy5 calculations by hand. Notice most of the results on the prop_main_Cy5
column are spurious. The only ones who make sense are those in which Cy5 value for that z_stack_id
is the total (e.g, FITC_TRITC/Cy5 makes no sense)
1 folder z_stack_id binary_layer n_blobs Cy5 prop_main_channel_Cy5
2 20180601_122650_896 1 Cy5 959 959 1
3 20180601_122650_896 1 FITC 16 959 0.016684046
4 20180601_122650_896 1 TRITC 499 959 0.520333681
5 20180601_122650_896 2 Cy5 225 225 1
6 20180601_122650_896 2 FITC 157 225 0.697777778
7 20180601_122650_896 2 TRITC 19 225 0.084444444
8 20180601_122650_896 1 FITC_Cy5 5 959 0.005213764
9 20180601_122650_896 1 FITC_TRITC 2 959 0.002085506
10 20180601_122650_896 1 TRITC_Cy5 301 959 0.313868613
11 20180601_122650_896 2 FITC_Cy5 34 225 0.151111111
12 20180601_122650_896 2 FITC_TRITC 8 225 0.035555556
13 20180601_122650_896 2 Triple 4 225 0.017777778
14 20180601_122650_896 2 TRITC_Cy5 8 225 0.035555556