By Peng Zhao | June 23, 2017
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) -0.2813564 0.7506792 -0.3748024 7.175483e-01
## x 1.0497947 0.1209829 8.6772142 2.421972e-05
##
## [[2]]
## [1] 0.9039547
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.01"
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.8572811 0.8394413 2.0457424 0.7039669 0.6301104
## 2 a c 0.8959828 0.8829807 -0.4980347 1.1075299 0.8278346
## 3 b a 0.8572811 0.8394413 -1.7063230 1.2177861 1.1080861
## 4 b c 0.7587823 0.7286301 -2.3392339 1.3405209 1.6855556
## 5 c a 0.8959828 0.8829807 0.9750007 0.8089920 0.6359260
## 6 c b 0.7587823 0.7286301 2.7515096 0.5660354 0.7362906
## Std.Error.slope t.intercept t.slope Pr.intercept Pr.slope
## 1 0.10155149 3.2466411 6.932118 0.011760072 1.205646e-04
## 2 0.13341763 -0.6016114 8.301226 0.564088289 3.343487e-05
## 3 0.17567302 -1.5398830 6.932118 0.162153516 1.205646e-04
## 4 0.26722349 -1.3878117 5.016479 0.202622852 1.031394e-03
## 5 0.09745451 1.5331984 8.301226 0.163768196 3.343487e-05
## 6 0.11283520 3.7369889 5.016479 0.005730036 1.031394e-03
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-mindr,
## title = {mindr: Convert Files Between Markdown or Rmarkdown Files and Mindmaps},
## author = {Peng Zhao},
## year = {2019},
## note = {R package version 1.2.3},
## url = {https://github.com/pzhaonet/mindr},
## }
## @Manual{R-bookdownplus,
## title = {bookdownplus: Generate Assorted Books and Documents with R 'bookdown' Package},
## author = {Peng Zhao},
## year = {2019},
## note = {R package version 1.5.8},
## url = {https://github.com/pzhaonet/bookdownplus},
## }
## @Manual{R-pinyin,
## title = {pinyin: Convert Chinese Characters into Pinyin, Sijiao, Wubi or Other
## Codes},
## author = {Peng Zhao},
## year = {2019},
## note = {R package version 1.1.6},
## url = {https://github.com/pzhaonet/pinyin},
## }
## @Manual{R-beginr,
## title = {beginr: Functions for R Beginners},
## author = {Peng Zhao},
## year = {2019},
## note = {R package version 0.1.8},
## url = {https://github.com/pzhaonet/beginr},
## }
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.