-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
113 lines (72 loc) · 3.89 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
<!-- load libraries -->
```{r , include = FALSE}
library(tidyverse)
library(TidyWrappers)
```
# TidyWrappers
<!-- badges: start -->
```{r , echo=FALSE , results='asis' , message=FALSE}
cat(
badger::badge_devel("cparsania/tidywrappers" , color = "blue"),
badger::badge_lifecycle()
)
```
<!-- badges: end -->
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`](https://www.tidyverse.org/packages/){target="_blank"} and more specifically [`dplyr`](https://dplyr.tidyverse.org){target="_blank"} (all credits to [Hadley Wickham](http://hadley.nz) and [RStudio](https://rstudio.com) 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`](https://dplyr.tidyverse.org){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`](https://vita.had.co.nz/papers/tidy-data.pdf){target="_blank"} and [`dplyr`](https://dplyr.tidyverse.org){target="_blank"} core and helper functions.
`TidyWrappers` contains set of functions, which save you little from writing and implementing core [`dplyr`](https://dplyr.tidyverse.org){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`](https://dplyr.tidyverse.org){target="_blank"} functions. See below in examples.
## Install
Install current version of TidyWrappers on your system.
```{r, eval=FALSE}
# install.packages("devtools")
library(devtools)
devtools::install_github("cparsania/TidyWrappers", dependencies = TRUE)
```
## Examples
```{r , message=FALSE}
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()
```
## Reference
Follow page [Reference](https://cparsania.github.io/TidyWrappers/reference/) to see full list of functions.