Skip to content

Commit

Permalink
fixes aggregate.sf;
Browse files Browse the repository at this point in the history
see #2375
  • Loading branch information
edzer committed Apr 10, 2024
1 parent a8d05dd commit f3cfecb
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions R/aggregate.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ aggregate.sf = function(x, by, FUN, ..., do_union = TRUE, simplify = TRUE,
a = aggregate(x[unlist(i), , drop = FALSE],
list(rep(seq_len(nrow(by)), lengths(i))), FUN, ...)
nrow_diff = nrow(by) - nrow(a)

if (is.matrix(a[[2]])) # https://github.com/r-spatial/sf/issues/2375
a = data.frame(a[1], as.data.frame(a[[2]]))

if(nrow_diff > 0) {
a_na = a[rep(NA, nrow(by)),] # 'top-up' missing rows
a_na[a$Group.1,] = a
Expand Down Expand Up @@ -82,9 +86,7 @@ aggregate.sf = function(x, by, FUN, ..., do_union = TRUE, simplify = TRUE,
else
paste0("Group.", seq_along(by))
agr[n] = "identity"
st_agr(x) = agr

x
st_set_agr(x, agr)
}
}

Expand Down

0 comments on commit f3cfecb

Please sign in to comment.