Skip to content

Commit d9f8d28

Browse files
authored
Merge pull request #126 from smped/devel
Respect column names in mutate
2 parents c2ff9bc + 1dc54e3 commit d9f8d28

2 files changed

Lines changed: 17 additions & 18 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Authors@R: c(
1010
person("Spencer", "Nystrom", role = c("ctb"),
1111
comment = c(ORCID = "0000-0003-1000-1579")),
1212
person("Pierre-Paul", "Axisa", role=c("ctb")),
13+
person("Stevie", "Pederson", role=c("ctb")),
1314
person("Michael", "Love", role = c("ctb","cre"),
1415
email = "michaelisaiahlove@gmail.com")
1516
)

R/dplyr-mutate.R

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,27 @@ mutate_mcols <- function(.data, .mutated) {
2222
}
2323

2424
if (!all(idx_mcols)) {
25-
if (is.null(mcols(.data))) {
26-
mcols(.data) <- S4Vectors::DataFrame(.mutated[!idx_mcols])
27-
} else {
28-
mcols(.data) <- S4Vectors::DataFrame(list(mcols(.data),
29-
.mutated[!idx_mcols]))
30-
}
25+
mcol_list <- c(as.list(mcols(.data)), .mutated[!idx_mcols])
26+
nm <- names(mcol_list)
27+
mcols(.data) <- S4Vectors::DataFrame(mcol_list)
28+
names(mcols(.data)) <- nm
3129
}
3230
.data
3331
}
3432

3533
# PPA grouped mutate speedup, 2025
3634
mutate_mcols_grp <- function(.data, dots) {
37-
35+
3836
# generate grouped df
3937
grps <- dplyr::group_vars(.data)
4038
df <- as.data.frame(dplyr::ungroup(.data))
4139
df <- dplyr::group_by(df,!!!rlang::syms(grps))
42-
40+
4341
mcols(.data) <- dplyr::mutate(df, !!!dots) %>%
4442
dplyr::ungroup() %>%
4543
dplyr::select(-tidyselect::any_of(c("start", "end", "width", "seqnames", "strand"))) %>%
4644
as("DataFrame")
47-
45+
4846
return(.data)
4947
}
5048

@@ -77,21 +75,21 @@ mutate_rng <- function(.data, dots) {
7775
mutate_mcols(.data, .mutated)
7876
}
7977

80-
# idea could simply dispatch to summarise here, and store
78+
# idea could simply dispatch to summarise here, and store
8179
# list columns, if the length is smaller then we can repeat,
82-
# otherwise we try to expand
80+
# otherwise we try to expand
8381
mutate_grp <- function(.data, dots) {
84-
82+
8583
inx <- .group_rows(.data)
8684
rng <- unname(S4Vectors::split(.data@delegate, .data@group_indices))
8785
rng <- S4Vectors::endoapply(rng, function(x) {
88-
mutate_rng(x, dots)
86+
mutate_rng(x, dots)
8987
})
90-
88+
9189
rng <- unlist(rng)[BiocGenerics::order(unlist(inx))]
9290
new(class(.data),
93-
delegate = rng,
94-
group_keys = .data@group_keys,
91+
delegate = rng,
92+
group_keys = .data@group_keys,
9593
group_indices = .data@group_indices,
9694
n = .data@n )
9795
}
@@ -176,11 +174,11 @@ mutate.DelegatingIntegerRanges <- mutate.DelegatingGenomicRanges
176174
#' @method mutate GroupedGenomicRanges
177175
#' @export
178176
mutate.GroupedGenomicRanges <- function(.data, ...) {
179-
177+
180178
dots <- set_dots_named(...)
181179
check_colnames(names(dots))
182180
core_cols <- names(dots) %in% c("start", "end", "width", "seqnames", "strand")
183-
181+
184182
# if any S4 columns in mcols use plyranges group mutate
185183
if (any(sapply(mcols(.data), isS4))) {
186184
message(

0 commit comments

Comments
 (0)