Skip to content

Commit a50f455

Browse files
committed
fix(clonalutils): set count to 0 if a group doesn't exist during comparison
1 parent 96dcdd4 commit a50f455

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

R/clonalutils.R

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ screp_subset <- function(screp, subset) {
314314
data2 <- data
315315
}
316316
data2 <- data2 %>%
317-
group_by(!!!syms(unique(c(id, groups)))) %>%
317+
group_by(!!!syms(unique(c(id, groups))), .drop = FALSE) %>%
318318
summarise(.n = n(), .groups = "drop")
319319

320320
if (!is.null(groups)) {
@@ -571,6 +571,10 @@ uniq <- function(group1, group2, ..., groups = NULL, data = NULL, id = NULL, in_
571571
expr <- paste0(expr, " & ", .bquote(g), " == 0")
572572
}
573573
stopifnot("`groups` must be provided when `in_form` is 'long'." = !is.null(groups) || in_form == "wide")
574+
if (in_form == "long") {
575+
data <- data[data[[groups]] %in% c(group1, group2, other_groups), , drop = FALSE]
576+
data[[groups]] <- factor(data[[groups]], levels = c(group1, group2, other_groups))
577+
}
574578
sel(expr, groups, data, id = id, in_form = in_form, within = within, return_ids = return_ids)
575579
}
576580

@@ -603,6 +607,10 @@ shared <- function(group1, group2, ..., groups = NULL, data = NULL, id = NULL, i
603607
expr <- paste0(expr, " & ", .bquote(g), " > 0")
604608
}
605609
stopifnot("`groups` must be provided when `in_form` is 'long'." = !is.null(groups) || in_form == "wide")
610+
if (in_form == "long") {
611+
data <- data[data[[groups]] %in% c(group1, group2, other_groups), , drop = FALSE]
612+
data[[groups]] <- factor(data[[groups]], levels = c(group1, group2, other_groups))
613+
}
606614
sel(expr, groups, data, id = id, in_form = in_form, within = within, return_ids = return_ids)
607615
}
608616

@@ -634,6 +642,10 @@ gt <- function(group1, group2, include_zeros = TRUE, groups = NULL, data = NULL,
634642
expr <- paste0(.bquote(group2), " > 0 & ", expr)
635643
}
636644
stopifnot("`groups` must be provided when `in_form` is 'long'." = !is.null(groups) || in_form == "wide")
645+
if (in_form == "long") {
646+
data <- data[data[[groups]] %in% c(group1, group2), , drop = FALSE]
647+
data[[groups]] <- factor(data[[groups]], levels = c(group1, group2))
648+
}
637649
sel(expr, groups, data, id = id, in_form = in_form, within = within, return_ids = return_ids)
638650
}
639651

@@ -665,6 +677,10 @@ ge <- function(group1, group2, include_zeros = TRUE, groups = NULL, data = NULL,
665677
expr <- paste0(.bquote(group2), " > 0 & ", expr)
666678
}
667679
stopifnot("`groups` must be provided when `in_form` is 'long'." = !is.null(groups) || in_form == "wide")
680+
if (in_form == "long") {
681+
data <- data[data[[groups]] %in% c(group1, group2), , drop = FALSE]
682+
data[[groups]] <- factor(data[[groups]], levels = c(group1, group2))
683+
}
668684
sel(expr, groups, data, id = id, in_form = in_form, within = within, return_ids = return_ids)
669685
}
670686

@@ -696,6 +712,10 @@ lt <- function(group1, group2, include_zeros = TRUE, groups = NULL, data = NULL,
696712
expr <- paste0(.bquote(group1), " > 0 & ", expr)
697713
}
698714
stopifnot("`groups` must be provided when `in_form` is 'long'." = !is.null(groups) || in_form == "wide")
715+
if (in_form == "long") {
716+
data <- data[data[[groups]] %in% c(group1, group2), , drop = FALSE]
717+
data[[groups]] <- factor(data[[groups]], levels = c(group1, group2))
718+
}
699719
sel(expr, groups, data, id = id, in_form = in_form, within = within, return_ids = return_ids)
700720
}
701721

@@ -727,6 +747,10 @@ le <- function(group1, group2, include_zeros = TRUE, groups = NULL, data = NULL,
727747
expr <- paste0(.bquote(group1), " > 0 & ", expr)
728748
}
729749
stopifnot("`groups` must be provided when `in_form` is 'long'." = !is.null(groups) || in_form == "wide")
750+
if (in_form == "long") {
751+
data <- data[data[[groups]] %in% c(group1, group2), , drop = FALSE]
752+
data[[groups]] <- factor(data[[groups]], levels = c(group1, group2))
753+
}
730754
sel(expr, groups, data, id = id, in_form = in_form, within = within, return_ids = return_ids)
731755
}
732756

@@ -755,6 +779,10 @@ eq <- function(group1, group2, groups = NULL, data = NULL, id = NULL, in_form =
755779
id <- id %||% ifelse(envtype == "tidy", "CTaa", "CloneID")
756780
expr <- paste0(.bquote(group1), " == ", .bquote(group2))
757781
stopifnot("`groups` must be provided when `in_form` is 'long'." = !is.null(groups) || in_form == "wide")
782+
if (in_form == "long") {
783+
data <- data[data[[groups]] %in% c(group1, group2), , drop = FALSE]
784+
data[[groups]] <- factor(data[[groups]], levels = c(group1, group2))
785+
}
758786
sel(expr, groups, data, id = id, in_form = in_form, within = within, return_ids = return_ids)
759787
}
760788

@@ -786,6 +814,10 @@ ne <- function(group1, group2, include_zeros = TRUE, groups = NULL, data = NULL,
786814
expr <- paste0(.bquote(group1), " > 0 & ", .bquote(group2), " > 0 & ", expr)
787815
}
788816
stopifnot("`groups` must be provided when `in_form` is 'long'." = !is.null(groups) || in_form == "wide")
817+
if (in_form == "long") {
818+
data <- data[data[[groups]] %in% c(group1, group2), , drop = FALSE]
819+
data[[groups]] <- factor(data[[groups]], levels = c(group1, group2))
820+
}
789821
sel(expr, groups, data, id = id, in_form = in_form, within = within, return_ids = return_ids)
790822
}
791823

0 commit comments

Comments
 (0)