Skip to content

Commit 71b60f1

Browse files
committed
New function, split_len() to split a split()-able object by lengths.
1 parent 0193075 commit 71b60f1

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: statnet.common
2-
Version: 4.13.0-507
2+
Version: 4.13.0-508
33
Date: 2025-12-16
44
Title: Common R Scripts and Utilities Used by the Statnet Project Software
55
Authors@R: c(

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export(skip_if_not_checking)
150150
export(snctrl)
151151
export(snctrl_names)
152152
export(snearPD)
153+
export(split_len)
153154
export(srcond)
154155
export(ssolve)
155156
export(statnet.cite.foot)

R/misc.utilities.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,3 +1382,27 @@ which_top_n <- function(x, n, tied = c("given", "all", "none")) {
13821382
tied = which(s1 != s2)
13831383
)
13841384
}
1385+
1386+
#' Split a list or some other `split()`-able object by lengths
1387+
#'
1388+
#' `split_len()` splits an object, such as a list or a data frame,
1389+
#' into subsets with specified lengths.
1390+
#'
1391+
#' @param x an object with a [split()] method.
1392+
#' @param l a vector of lengths of the subsets.
1393+
#'
1394+
#' @return A list with elements of the same type as `x`.
1395+
#'
1396+
#' @examples
1397+
#' x <- 1:10
1398+
#' l <- 1:4
1399+
#'
1400+
#' o <- split_len(x, l)
1401+
#'
1402+
#' stopifnot(identical(lengths(o), l))
1403+
#' stopifnot(identical(unlist(o), x))
1404+
#'
1405+
#' @export
1406+
split_len <- function(x, l) {
1407+
unname(split(x, factor(rep.int(seq_along(l), l), levels = seq_along(l))))
1408+
}

man/split_len.Rd

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)