knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
library(tidyverse) library(TidyWrappers)
cat( badger::badge_devel("cparsania/tidywrappers" , color = "blue"), badger::badge_lifecycle() )
TidyWrappers is an R package to deal with an object tbl
. It provides handy wrapper functions on top of dplyr
verbs to get, count, convert, keep, remove and replace data from a tbl
object.
The way R is being used, has changed drastically in last few years due to the availability of tidyverse
{target="_blank"} and more specifically dplyr
{target="_blank"} (all credits to Hadley Wickham and RStudio team). Rich documentation, stable release and excellent functionalities of these packages have provoked significant number of R users to switch from traditional dataframes
to intelligible data structure, tibble
(aka tbl
). dplyr
{target="_blank"} provides users with rich utilities like select
, mutate
, filter
, group_by
, summarise
and arrange
for easy manipulation of a tbl
. However, for newbies it takes a while to become familiar with tidy data
{target="_blank"} and dplyr
{target="_blank"} core and helper functions.
TidyWrappers
contains set of functions, which save you little from writing and implementing core dplyr
{target="_blank"} verbs while manipulating data from tbl
. Additionaly, redundant lines of code can be avoided using functions given in TidyWrappers
. Current version of TidyWrappers
containes more than 30 functions, which are wrapped on top of various dplyr
{target="_blank"} functions. See below in examples.
Install current version of TidyWrappers on your system.
# install.packages("devtools") library(devtools) devtools::install_github("cparsania/TidyWrappers", dependencies = TRUE)
library(dplyr) library(TidyWrappers) library(magrittr) tbl <- tibble::tibble(a = letters[1:6], b = NA_character_, x = c(0,1,2,0,0,NA) , y = c(0,1,2,0,3,5) , z = c(0,1,2,0,3,5) ) tbl ## keep rows having 0 across all numeric columns. # using dplyr tbl %>% dplyr::filter_if(is.numeric , dplyr::all_vars( . == 0) ) # using TidyWrappers tbl %>% TidyWrappers::tbl_keep_rows_zero_all() ## remove rows having 0 across all numeric columns. # using dplyr tbl %>% dplyr::filter_if(is.numeric , dplyr::any_vars( . > 0) ) # using TidyWrappers tbl %>% TidyWrappers::tbl_remove_rows_zero_all() ## Convert log2 to all numeric columns optionally adding numeric fraction to original values. # using dplyr tbl %>% dplyr::mutate_if(is.numeric , ~ log2 (. + 1)) # using TidyWrappers tbl %>% TidyWrappers::tbl_convert_log2(frac = 1) ## Remove columns / variables having atleaset one NA # using dplyr tbl %>% dplyr::select_if( ~ ( (is.na(.)) %>% any(., na.rm = T) %>% `!`) ) # using TidyWrappers tbl %>% TidyWrappers::tbl_remove_vars_NA_any() ## Remove columns / variables having all values are NA # using dplyr tbl %>% dplyr::select_if( ~ ( (is.na(.)) %>% all(., na.rm = T) %>% `!`) ) # using TidyWrappers tbl %>% TidyWrappers::tbl_remove_vars_NA_all()
Follow page Reference to see full list of functions.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.