Commit 22e64bed authored by houyun's avatar houyun
Browse files

can select by regex on rows

parent f137be7b
Loading
Loading
Loading
Loading
+19 −11
Original line number Diff line number Diff line
@@ -348,7 +348,8 @@ check_env_ctrl <- function(env, env_ctrl, env_select) {
regex_select <- function(prefix = "",
                         suffix = NULL,
                         regex = NULL,
                         ...) {
                         ...,
                         byrow = FALSE) {
  if (is.null(prefix %||% suffix %||% regex)) {
    stop("At least one pattern is not null.", call. = FALSE)
  }
@@ -366,31 +367,38 @@ regex_select <- function(prefix = "",
    if (empty(data)) {
      stop("Empty data.", call. = FALSE)
    }

    if (isTRUE(byrow)) {
      id <- rownames(data)[Reduce("|", lapply(regex, function(.regex) {
        grepl(pattern = .regex, x = rownames(data), ...)
      }))]
      data[id, , drop = FALSE]
    } else {
      id <- names(data)[Reduce("|", lapply(regex, function(.regex) {
        grepl(pattern = .regex, x = names(data), ...)
      }))]

      data[id]
    }
  }
}

#' @rdname regex_select
#' @export
prefix_with <- function(data, chr = "", ...) {
  FUN <- regex_select(prefix = chr, ...)
prefix_with <- function(data, chr = "", ..., byrow = FALSE) {
  FUN <- regex_select(prefix = chr, ..., byrow = byrow)
  FUN(data)
}

#' @rdname regex_select
#' @export
suffix_with <- function(data, chr = "", ...) {
  FUN <- regex_select(suffix = chr, ...)
suffix_with <- function(data, chr = "", ..., byrow = FALSE) {
  FUN <- regex_select(suffix = chr, ..., byrow = byrow)
  FUN(data)
}

#' @rdname regex_select
#' @export
contain_with <- function(data, chr = "", ...) {
  FUN <- regex_select(regex = chr, ...)
contain_with <- function(data, chr = "", ..., byrow = FALSE) {
  FUN <- regex_select(regex = chr, ..., byrow = byrow)
  FUN(data)
}
+1 −1
Original line number Diff line number Diff line
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/as-tibble.R
% Please edit documentation in R/as-md-tbl.R
\name{as_md_tbl}
\alias{as_md_tbl}
\alias{as_md_tbl.matrix_data}
+4 −4
Original line number Diff line number Diff line
@@ -8,13 +8,13 @@
\title{Subset columns using regular expression
Subset columns from a data frame if column names match the regular pattern.}
\usage{
regex_select(prefix = "", suffix = NULL, regex = NULL, ...)
regex_select(prefix = "", suffix = NULL, regex = NULL, ..., byrow = FALSE)

prefix_with(data, chr = "", ...)
prefix_with(data, chr = "", ..., byrow = FALSE)

suffix_with(data, chr = "", ...)
suffix_with(data, chr = "", ..., byrow = FALSE)

contain_with(data, chr = "", ...)
contain_with(data, chr = "", ..., byrow = FALSE)
}
\arguments{
\item{prefix}{prefix of column names.}