Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract numbers and words added #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export(tbl_convert_log10)
export(tbl_convert_log2)
export(tbl_convert_row_zscore)
export(tbl_count_zero_vars)
export(tbl_extract_numbers)
export(tbl_extract_words)
export(tbl_get_zero_records)
export(tbl_get_zero_vars)
export(tbl_keep_greater_than_or_equal_all)
Expand All @@ -31,7 +33,9 @@ importFrom(dplyr,mutate_if)
importFrom(dplyr,select)
importFrom(dplyr,select_if)
importFrom(purrr,as_mapper)
importFrom(rlang,ensym)
importFrom(stringi,stri_rand_strings)
importFrom(stringr,str_extract)
importFrom(stringr,str_replace_all)
importFrom(tibble,rownames_to_column)
importFrom(tidyr,pivot_longer)
Expand Down
46 changes: 46 additions & 0 deletions R/wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -604,10 +604,56 @@ tbl_convert_row_zscore <- function(tbl, scale = TRUE, center = TRUE){

}

#' Extract numeric values from alpha-numeric values
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to improve on title. Instead of values, vars might be amore appropriate

#'
#' @param tbl a tbl.
#' @param var column name, the column fom which numbers are to be extracted
#'
#' @return a tbl
#' @export
#'
#' @importFrom dplyr mutate
#' @importFrom stringr str_extract
#' @importFrom purrr as_mapper
#' @importFrom rlang ensym
#' @examples
#' \dontrun{
#' alpha_num <- cbind(paste0(LETTERS[1:10], 1:10), letters[1:10]) %>% as_tibble()
#' alpha_num %>% tbl_extract_numbers(V1)
#' }
tbl_extract_numbers <- function(tbl, var){

vars <- ensym(var)
vars_num <- paste0(vars,"_numeric", sep="")
mm <- purrr::as_mapper(~ ..1 %>% dplyr::mutate(!!as.symbol(vars_num) := stringr::str_extract(string = !!..2, pattern = "[:digit:]+")))
tbl %>% mm(vars)
}



#' Extract words/alphabets from alpha-numeric values
#'
#' @param tbl a tbl
#' @param var column name, the column fom which words are to be extracted
#'
#' @return a tbl
#' @export
#' @importFrom dplyr mutate
#' @importFrom stringr str_extract
#' @importFrom purrr as_mapper
#' @importFrom rlang ensym
#' @examples
#' \dontrun{
#' alpha_num <- cbind(paste0(LETTERS[1:10], 1:10), letters[1:10]) %>% as_tibble()
#' alpha_num %>% tbl_extract_words(V1)
#' }
#'
tbl_extract_words <- function(tbl, var){
vars <- ensym(var)
vars_num <- paste0(vars,"_alpha", sep="")
mm <- purrr::as_mapper(~ ..1 %>% dplyr::mutate(!!as.symbol(vars_num) := stringr::str_extract(string = !!..2, pattern = "[:alpha:]+")))
tbl %>% mm(vars)
}



25 changes: 25 additions & 0 deletions man/tbl_extract_numbers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions man/tbl_extract_words.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.