R package beginr released on CRAN

I’m glad to announce that ‘beginr’ (Zhao 2017) is now on CRAN. This post is a brief documentation of ‘beginr 0.1.0’. Subsequent updates can be found on the GitHub site of beginr.

Introduction

‘beginr’ is a collection of useful functions for R beginners, including hints for the arguments of the ‘plot()’ function, self-defined functions for error bars, user-customized pair plots and hist plots, enhanced linear regression figures, etc.. This package could be helpful to R experts as well.

Installation

# stable version on CRAN
install.package("beginr")
# or development version on GitHub
devtools::install_github("pzhaonet/beginr")

Quick Start

Functions as Memos

If you often forget the options for pch, lty, type, and col when you plot(), you can run plotpch(), plotlty(), plottype(), plotcolors(), and plotcolorbar(). No need to search the internet any more.

beginr::plotpch()

beginr::plotlty()

beginr::plottype()

beginr::plotcolors()

beginr::plotcolorbar()

Functions for Easy Plotting

Linear regression is often used in scientific work, but it is annoying to display the results. In R you have to run lm(), plot(), abline(), text() and so on and so forth. Now with ‘beginr’ you only have to run plotlm().

x <- 1:10
y <- 1:10 + rnorm(10)
beginr::plotlm(x, y)

## [[1]]
##              Estimate Std. Error  t value     Pr(>|t|)
## (Intercept) 1.2404796  0.6569697 1.888184 0.0956930298
## x           0.7604808  0.1058803 7.182461 0.0000940501
## 
## [[2]]
## [1] 0.8657442

If you want to display the distribution of a data-set, use plothist(), which gives you a histogram with markers of the median, mean, quantiles, standard deviation, sample number and the skewness.

x <- rnorm(10000)
beginr::plothist(x)

I like pairs(), and ‘beginr’ gives more powerful features to plotpairs() and plotpairs2().

df <- data.frame(a = 1:10, b = 1:10 + rnorm(10), c = 1:10 + rnorm(10))
beginr::plotpairs(df)

beginr::plotpairs2(df)
## [1] "p<0.01"
## [1] "p<0.01"
## [1] "p<0.05"

I often have to plot one independent variable (x) and multiple dependent variables (y1, y2, …, yn) in one 2-D coordinate system, or one dependent variable (y) against multiple independent variables (x1, x2, …, xn) with their error bars. Use dfplot() or dfplot2() in ‘beginr’.

x <- seq(0, 2 * pi, length.out = 100)
y <- data.frame(sin(x), cos(x))
yerror <- data.frame(abs(rnorm(100, sd = 0.3)), abs(rnorm(100, sd = 0.1)))
beginr::dfplot(x, y, yerror = yerror)

beginr::dfplot2(y, x, xerror = yerror)

If you would like to add errorbars only, then use errorbar().

x <- seq(0, 2 * pi, length.out = 100)
y <- sin(x)
plot(x, y, type = "l")
beginr::errorbar(x, y, yupper = 0.1, ylower = 0.1)

Functions for Data Frames

lmdf() performs the linear regression between every two columns in a data frame.

df <- data.frame(a = 1:10, b = 1:10 + rnorm(10), c = 1:10 + rnorm(10))
beginr::lmdf(df)
##   x y r.squared adj.r.squared  intercept     slope Std.Error.intercept
## 1 a b 0.9546449     0.9489755  0.6171705 0.9206305           0.4402132
## 2 a c 0.9103184     0.8991082  0.1910661 0.9653818           0.6647211
## 3 b a 0.9546449     0.9489755 -0.3905198 1.0369469           0.5028284
## 4 b c 0.8688661     0.8524744 -0.1853958 1.0009547           0.8651003
## 5 c a 0.9103184     0.8991082  0.3130807 0.9429621           0.6509973
## 6 c b 0.8688661     0.8524744  0.9058546 0.8680374           0.7417362
##   Std.Error.slope t.intercept   t.slope Pr.intercept     Pr.slope
## 1      0.07094678   1.4019810 12.976354    0.1985086 1.178689e-06
## 2      0.10712950   0.2874379  9.011353    0.7810821 1.836021e-05
## 3      0.07991050  -0.7766462 12.976354    0.4597122 1.178689e-06
## 4      0.13748347  -0.2143055  7.280546    0.8356704 8.548211e-05
## 5      0.10464157   0.4809247  9.011353    0.6434474 1.836021e-05
## 6      0.11922696   1.2212626  7.280546    0.2567535 8.548211e-05

There are two functions as improvements of tapply() for factor calculation.

beginr::tapplydf()
beginr::tapplydfv()

Functions for Reading and Writing files

readdir() reads multiple files into a list.

beginr::readdir()

writefile() avoids overwriting the files which already exist.

beginr::writefile()

list2ascii() saves a list as a text file.

alist <- list(a = 1:10, b = letters)
beginr::list2ascii(alist)

Functions for Packages

bib() creates bibliographic entries as texts or a file (‘.bib’).

beginr::bib(pkg = c("mindr", "bookdownplus", "pinyin", "beginr"))
## @Manual{R-beginr,
##   title = {beginr: Functions for R Beginners},
##   author = {Peng Zhao},
##   year = {2018},
##   note = {R package version 0.1.3},
##   url = {https://github.com/pzhaonet/beginr},
## }
## @Manual{R-bookdownplus,
##   title = {bookdownplus: Generate Assorted Books and Documents with R 'bookdown' Package},
##   author = {Peng Zhao},
##   year = {2018},
##   note = {R package version 1.5.2.1},
##   url = {https://github.com/pzhaonet/bookdownplus},
## }
## @Manual{R-mindr,
##   title = {mindr: Convert Files Between Markdown or Rmarkdown Files and Mindmaps},
##   author = {Peng Zhao},
##   year = {2018},
##   note = {R package version 1.1.9},
##   url = {https://github.com/pzhaonet/mindr},
## }
## @Manual{R-pinyin,
##   title = {pinyin: Convert Chinese Characters into Pinyin, Sijiao, Wubi or Other
## Codes},
##   author = {Peng Zhao},
##   year = {2018},
##   note = {R package version 1.1.4},
##   url = {https://CRAN.R-project.org/package=pinyin},
## }

plotpkg() displays a figure showing the downloads of packages.

beginr::plotpkg("beginr", from = "2017-06-23")

rpkg() creates a new R package in an easy way.

beginr::rpkg()

References

Zhao, Peng. 2017. Beginr: Functions for R Beginners. https://github.com/pzhaonet/beginr.

comments powered by Disqus