6 min read

beginr: an R package for beginners in R language - 帮助初学者学习 R 语言

Links: steemit.com, cnsteem.com, busy.org, steemdb.com, steemd.com

This post was written bilingually, in English and Chinese. If you use the tool I introduced yesterday, you are able to read both languages!

本文用英文和中文写作,中文在后半部分。

Introduction

Recently I have introduced three open-source projects on Utopian.io, which I developed with R language. They are:

  • bookdownplus: an R package that convert your steem posts into an elegant book,
  • mindr: an R package that creates a mind map from your steem posts, and
  • pinyin: an R package that give your the ability to read Chinese characters.

Thanks to the attention of many readers, who are much interested in these packages.

However, they have to learn R language.

R is often difficult for beginners, but don’t worry. R can be fun and easy if you use the beginr package, which I developed for R beginners.

Features

beginr provides many useful functions, including:

  • some functions which help the beginners recall some commands in R, such as the color codes and the plot shape codes. You don’t have to memorize that ‘1’ is an empty circle and ‘20’ is a filled one.
  • some wrapped plotting functions, which can plot complex figures.
  • some functions which can easily read and safely write data files, or process dataframes.

Furthermore, the source codes of beginr package were written on beginners’ level with the most commonly used functions. Any beginner can simply follow the source code so as to get ideas to write their own codes and even develop their own packages.

Quick Start

Installation of R

Download R and install it.

Installation of beginr

In R environment, run the following codes to install beginr.

# stable version:
install.packages("beginr")
# or development version:
devtools::install_github("pzhaonet/beginr")

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()

img

beginr::plotlty()

img

beginr::plottype()

img

beginr::plotcolors()

img

beginr::plotcolorbar()

img

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)

img

## [[1]]
##               Estimate Std. Error   t value     Pr(>|t|)
## (Intercept) -0.8826021 0.59514844 -1.482995 1.763634e-01
## x            1.1693736 0.09591686 12.191533 1.899987e-06
## 
## [[2]]
## [1] 0.9489254

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)

img

##     para         value
## 1    min -3.9594329262
## 2     1q -0.6743155802
## 3 median  0.0151220580
## 4     3q  0.6806285656
## 5    max  4.1576382225
## 6  lower -2.7065331755
## 7  upper  2.7128461609
## 8   mean -0.0005589431
## 9     sd  1.0064399683

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)

img

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

img

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)

img

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

img

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)

img

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.9630442     0.9584247 -0.7901086 1.1271749           0.4843895
## 2 a c 0.8568806     0.8389907 -0.3427519 1.0425054           0.9346580
## 3 b a 0.9630442     0.9584247  0.8783157 0.8543876           0.3749251
## 4 b c 0.8157952     0.7927696  0.6004724 0.8856059           0.9426967
## 5 c a 0.8568806     0.8389907  1.0688792 0.8219436           0.7466774
## 6 c b 0.8157952     0.7927696  0.4432909 0.9211717           0.9729768
##   Std.Error.slope t.intercept   t.slope Pr.intercept     Pr.slope
## 1      0.07806644  -1.6311430 14.438661   0.14150586 5.177385e-07
## 2      0.15063377  -0.3667137  6.920795   0.72334232 1.219459e-04
## 3      0.05917360   2.3426432 14.438661   0.04722045 5.177385e-07
## 4      0.14878374   0.6369730  5.952303   0.54193651 3.410966e-04
## 5      0.11876434   1.4315142  6.920795   0.19016727 1.219459e-04
## 6      0.15475888   0.4556027  5.952303   0.66078732 3.410966e-04

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 = {2017},
##   note = {R package version 0.1.0},
##   url = {https://github.com/pzhaonet/beginr},
## }
## @Manual{R-bookdownplus,
##   title = {bookdownplus: Generate Varied Books and Documents with R 'bookdown' Package},
##   author = {Peng Zhao},
##   year = {2017},
##   note = {R package version 1.2.2},
##   url = {https://github.com/pzhaonet/bookdownplus},
## }
## @Manual{R-mindr,
##   title = {mindr: Convert Files Between Markdown or Rmarkdown Files and Mindmaps},
##   author = {Peng Zhao},
##   year = {2017},
##   note = {R package version 1.1.0},
##   url = {https://github.com/pzhaonet/mindr},
## }
## @Manual{R-pinyin,
##   title = {pinyin: Convert Chinese Characters into Pinyin},
##   author = {Peng Zhao},
##   year = {2017},
##   note = {R package version 1.1.0},
##   url = {https://github.com/pzhaonet/pinyin},
## }

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

beginr::rpkg()

https://github.com/pzhaonet/beginr

Proof of Work

In my profile on Utopian.io, the github repo beginr is listed in my project page, which is show in the screenshot as follows.

beginr.jpg

The login page on github.com is shown in the following screenshot:

beginr2.jpg


简介

最近,我介绍了三个开源项目,都是我用 R 语言开发的,包括:

  • bookdownplus: 可以把你在 steem 上的帖子做成一本漂亮的电子书,
  • mindr: 可以把你的 steem 帖子转换成思维导图,或者反过来,把思维导图转换成 markdown 格式的帖子大纲,
  • pinyin: 可以把汉字转换成拼音。

感谢大家的关注。很多朋友对这些项目产生了兴趣。然而,要使用这些功能,就得先学会 R 语言。

R 语言入门并不容易。但是别担心,有捷径。我专门为 R 初学者开发了一个名为 beginr 的扩展包。

使用 beginr 扩展包,有些 R 指令你就不用记忆了。比如说,在作图的时候,’1’ 表示空心原点,’20’ 表示实心原点,还有各种奇形怪状的点,不好记。更不要说各种颜色代码了。beginr 里提供了一些函数,很方便地给你贴心提示。还有很多其他函数,这里就不详细介绍了,请看上面的英文示例部分。

最重要的是,beginr 本身的源代码都是用最简单最基础的 R 指令写成了,本身就是个示范代码集。初学者只需照猫画虎,跟着 beginr 的帮助文件和源代码,就可以很快学到 beginr 里的各种功能是如何实现的。

希望 beginr 能助你早日入门,跟我一起享受 R 语言的精彩世界。



Posted on Utopian.io - Rewarding Open Source Contributors