Coder Social home page Coder Social logo

tidywrappers's Introduction

TidyWrappers

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.

Motivation

The way R is being used, has changed drastically in last few years due to the availability of tidyverse and more specifically dplyr (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 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 and dplyr core and helper functions.

TidyWrappers contains set of functions, which save you little from writing and implementing core dplyr 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 functions. See below in examples.

Install

Install current version of TidyWrappers on your system.

# install.packages("devtools")
library(devtools)
devtools::install_github("cparsania/TidyWrappers", dependencies = TRUE)

Examples

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
#> # A tibble: 6 x 5
#>   a     b         x     y     z
#>   <chr> <chr> <dbl> <dbl> <dbl>
#> 1 a     <NA>      0     0     0
#> 2 b     <NA>      1     1     1
#> 3 c     <NA>      2     2     2
#> 4 d     <NA>      0     0     0
#> 5 e     <NA>      0     3     3
#> 6 f     <NA>     NA     5     5
  
  ## keep rows having  0 across all numeric columns. 
  
  # using dplyr
  tbl %>% dplyr::filter_if(is.numeric , dplyr::all_vars( . == 0) )
#> # A tibble: 2 x 5
#>   a     b         x     y     z
#>   <chr> <chr> <dbl> <dbl> <dbl>
#> 1 a     <NA>      0     0     0
#> 2 d     <NA>      0     0     0
  
  # using TidyWrappers
  tbl %>% TidyWrappers::tbl_keep_rows_zero_all()
#> # A tibble: 2 x 5
#>   a     b         x     y     z
#>   <chr> <chr> <dbl> <dbl> <dbl>
#> 1 a     <NA>      0     0     0
#> 2 d     <NA>      0     0     0

  ## remove rows having  0 across all numeric columns. 
  
  # using dplyr 
  tbl %>% dplyr::filter_if(is.numeric , dplyr::any_vars( . > 0) )
#> # A tibble: 4 x 5
#>   a     b         x     y     z
#>   <chr> <chr> <dbl> <dbl> <dbl>
#> 1 b     <NA>      1     1     1
#> 2 c     <NA>      2     2     2
#> 3 e     <NA>      0     3     3
#> 4 f     <NA>     NA     5     5
  
  # using TidyWrappers 
  tbl %>% TidyWrappers::tbl_remove_rows_zero_all()
#> # A tibble: 4 x 5
#>   a     b         x     y     z
#>   <chr> <chr> <dbl> <dbl> <dbl>
#> 1 b     <NA>      1     1     1
#> 2 c     <NA>      2     2     2
#> 3 e     <NA>      0     3     3
#> 4 f     <NA>     NA     5     5
  
  ## Convert log2 to all numeric columns optionally adding numeric fraction to original values. 
  
  # using dplyr 
  tbl %>% dplyr::mutate_if(is.numeric , ~ log2 (. +  1))
#> # A tibble: 6 x 5
#>   a     b         x     y     z
#>   <chr> <chr> <dbl> <dbl> <dbl>
#> 1 a     <NA>   0     0     0   
#> 2 b     <NA>   1     1     1   
#> 3 c     <NA>   1.58  1.58  1.58
#> 4 d     <NA>   0     0     0   
#> 5 e     <NA>   0     2     2   
#> 6 f     <NA>  NA     2.58  2.58
 
  # using TidyWrappers
  tbl %>% TidyWrappers::tbl_convert_log2(frac = 1)
#> # A tibble: 6 x 5
#>   a     b         x     y     z
#>   <chr> <chr> <dbl> <dbl> <dbl>
#> 1 a     <NA>   0     0     0   
#> 2 b     <NA>   1     1     1   
#> 3 c     <NA>   1.58  1.58  1.58
#> 4 d     <NA>   0     0     0   
#> 5 e     <NA>   0     2     2   
#> 6 f     <NA>  NA     2.58  2.58

  
  ## Remove columns / variables  having atleaset one NA
  
  # using dplyr
  tbl %>% dplyr::select_if( ~ ( (is.na(.)) %>% any(., na.rm = T)  %>% `!`) ) 
#> # A tibble: 6 x 3
#>   a         y     z
#>   <chr> <dbl> <dbl>
#> 1 a         0     0
#> 2 b         1     1
#> 3 c         2     2
#> 4 d         0     0
#> 5 e         3     3
#> 6 f         5     5
  
  # using TidyWrappers
  tbl %>% TidyWrappers::tbl_remove_vars_NA_any()
#> # A tibble: 6 x 3
#>   a         y     z
#>   <chr> <dbl> <dbl>
#> 1 a         0     0
#> 2 b         1     1
#> 3 c         2     2
#> 4 d         0     0
#> 5 e         3     3
#> 6 f         5     5
  
  ## Remove columns / variables  having all values are NA
  
  # using dplyr
  tbl %>% dplyr::select_if( ~ ( (is.na(.)) %>% all(., na.rm = T)  %>% `!`) ) 
#> # A tibble: 6 x 4
#>   a         x     y     z
#>   <chr> <dbl> <dbl> <dbl>
#> 1 a         0     0     0
#> 2 b         1     1     1
#> 3 c         2     2     2
#> 4 d         0     0     0
#> 5 e         0     3     3
#> 6 f        NA     5     5
  
  # using TidyWrappers
  tbl %>% TidyWrappers::tbl_remove_vars_NA_all()
#> # A tibble: 6 x 4
#>   a         x     y     z
#>   <chr> <dbl> <dbl> <dbl>
#> 1 a         0     0     0
#> 2 b         1     1     1
#> 3 c         2     2     2
#> 4 d         0     0     0
#> 5 e         0     3     3
#> 6 f        NA     5     5

Reference

Follow page Reference to see full list of functions.

tidywrappers's People

Contributors

cparsania avatar sethiyap avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

sethiyap

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.