Extract the proportion of cells in each cluster in a `tof_tibble`.
Source:R/feature_extraction.R
tof_extract_proportion.Rd
This feature extraction function allows you to calculate the proportion of cells in each cluster in a `tof_tibble` - either overall or when broken down into subgroups using `group_cols`.
Usage
tof_extract_proportion(
tof_tibble,
cluster_col,
group_cols = NULL,
format = c("wide", "long")
)
Arguments
- tof_tibble
A `tof_tbl` or a `tibble`.
- cluster_col
An unquoted column name indicating which column in `tof_tibble` stores the cluster ids of the cluster to which each cell belongs. Cluster labels can be produced via any method the user chooses - including manual gating, any of the functions in the `tof_cluster_*` function family, or any other method.
- group_cols
Unquoted column names representing which columns in `tof_tibble` should be used to break the rows of `tof_tibble` into subgroups for the feature extraction calculation. Defaults to NULL (i.e. performing the extraction without subgroups).
- format
A string indicating if the data should be returned in "wide" format (the default; each cluster proportion is given its own column) or in "long" format (each cluster proportion is provided as its own row).
Value
A tibble.
If format == "wide", the tibble will have 1 row for each combination of the grouping variables provided in `group_cols` and one column for each grouping variable as well as one column for the proportion of cells in each cluster. The names of each column containing cluster proportions is obtained using the following pattern: "prop@{cluster_id}".
If format == "long", the tibble will have 1 row for each combination of the grouping variables in `group_cols` and each cluster id (i.e. level) in `cluster_col`. It will have one column for each grouping variable, one column for the cluster ids, and one column (`prop`) containing the cluster proportions.
See also
Other feature extraction functions:
tof_extract_central_tendency()
,
tof_extract_emd()
,
tof_extract_features()
,
tof_extract_jsd()
,
tof_extract_threshold()
Examples
sim_data <-
dplyr::tibble(
cd45 = rnorm(n = 1000),
cd38 = rnorm(n = 1000),
cd34 = rnorm(n = 1000),
cd19 = rnorm(n = 1000),
cluster_id = sample(letters, size = 1000, replace = TRUE),
patient = sample(c("kirby", "mario"), size = 1000, replace = TRUE),
stim = sample(c("basal", "stim"), size = 1000, replace = TRUE)
)
# extract proportion of each cluster in each patient in wide format
tof_extract_proportion(
tof_tibble = sim_data,
cluster_col = cluster_id,
group_cols = patient
)
#> # A tibble: 2 × 27
#> patient `prop@a` `prop@b` `prop@c` `prop@d` `prop@e` `prop@f` `prop@g`
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 kirby 0.0327 0.0306 0.0347 0.0367 0.0408 0.0367 0.0490
#> 2 mario 0.0412 0.0294 0.0529 0.0471 0.0333 0.0333 0.0294
#> # ℹ 19 more variables: `prop@h` <dbl>, `prop@i` <dbl>, `prop@j` <dbl>,
#> # `prop@k` <dbl>, `prop@l` <dbl>, `prop@m` <dbl>, `prop@n` <dbl>,
#> # `prop@o` <dbl>, `prop@p` <dbl>, `prop@q` <dbl>, `prop@r` <dbl>,
#> # `prop@s` <dbl>, `prop@t` <dbl>, `prop@u` <dbl>, `prop@v` <dbl>,
#> # `prop@w` <dbl>, `prop@x` <dbl>, `prop@y` <dbl>, `prop@z` <dbl>
# extract proportion of each cluster in each patient in long format
tof_extract_proportion(
tof_tibble = sim_data,
cluster_col = cluster_id,
group_cols = patient,
format = "long"
)
#> # A tibble: 52 × 3
#> patient cluster_id prop
#> <chr> <chr> <dbl>
#> 1 kirby a 0.0327
#> 2 kirby b 0.0306
#> 3 kirby c 0.0347
#> 4 kirby d 0.0367
#> 5 kirby e 0.0408
#> 6 kirby f 0.0367
#> 7 kirby g 0.0490
#> 8 kirby h 0.0429
#> 9 kirby i 0.0469
#> 10 kirby j 0.0306
#> # ℹ 42 more rows