R language function, then study notes 5

Programming completion function using Tidyverse

(Refer to the seniors home Xiang notes)
using 1.magrittr package
there are a lot of pipes can reduce the function ,, code development time and improve code readability and maintainability
1.1 four kinds of Pipeline
1.1.1 commonly used:%>% the most popular nested, nested rightward
6
example: requirements

  1. Take 10,000 random numbers with normal distribution
  2. Seeking absolute value of 10000, while multiplied by 50
  3. The result of the composition 100 * 100 square box column
    4. Calculate the mean square in each row, and rounded to an integer of
    5 to 7 remainder is divided by the number of results, and draw the remainder of the histogram
library(tseries)
library(zoo)
library(lmtest)
library(magrittr)
library(pder)
library(texreg)
library(tidyverse)
library(stargazer)

My preference is to use the package if what I basically put all the packages, put together, put at the head of the document, but many seniors and good habits which is used, which is called, may be I was too dishes .

set.seed(123)
rnorm(n=10000) %>% abs %>% '*'(50) %>% matrix(ncol=100) %>% rowMeans %>% round %>% '%%' (7) %>% hist

in other words:

x%>% f (y) is equivalent to f (x, y)

y%>% f (x,., z) is equivalent to f (x, y, z)

% T 1.1.2>%
1. Take normal distribution random numbers 10000
absolute value of 2. The required number 10000, while the multiplier 50
3. square box of the result of the composition of 100 * 100
4. Calculate mean square in each row, and rounded to an integer of
5 to 7 remainder is divided by the number of results, and draw the remainder of the histogram
6. remainder sum

set.seed(123)
rnorm(n=10000) %>% abs %>% '*'(50) %>% matrix(ncol=100) %>% rowMeans %>% round %>% '%%' (7) %T>% hist %>% sum

% T>% left operator, in fact, function and%>% is basically the same, except that it is the value as the value passed to the left, not the right value. This situation is a lot of usage scenarios, for example, you are in the middle of the process data processing, need a printout or picture output, then the whole process will be interrupted by the operator to the left, you can solve this problem.

1.1.3 %\(% 解释操作符(exposition pipe-operator) %\)% 的作用是把左侧数据的属性名传给右侧,让右侧的调用函数直接通过名字,就可以获取左侧的数据。比如,我们获得一个data.frame类型的数据集,通过使用 %\(%,在右侧的函数中可以直接使用列名操作数据。 其实就是传递属性名的吧 ```{r} attach(iris) iris %>% subset(Sepal.Length>mean(Sepal.Length)) %\)% cor(Sepal.Length,Sepal.Width)
[1] 0.3361992
```
这样子可以省略.$a

1.1.4 %<>%
%<>% 复合赋值操作符(compound assignment pipe-operator)

%<>%复合赋值操作符, 功能与 %>% 基本是一样的,对了一项额外的操作,就是把结果写到左侧对象。比如,我们需要对一个数据集进行排序,那么需要获得排序的结果,用%<>%就是非常方便的。

需要注意一下 %<>% 必须要用在第一个管道的对象处,才能完成赋值的操作,如果不是左侧第一个位置,那么赋值将不起作用。

可以参考博客:https://blog.csdn.net/kmd8d5r/article/details/82881559
写的比较详细
1.1.5
%>%对代码块的传递

iris %>% (
  function(x){
    if(nrow(x)>2)
      bind_rows(x %>% head(1),x %>% tail(1))
    else x
  }
)

%>%对函数的传递

2 read_*读入数据

2.1 read_*文档
在学长的笔记中指出:Yihui在blogdown包中采用read_utf8{xfun}而非read_file,保证了代码好似utf-8的格式录入,read_utf8虽然不是Tidyverse集成包中的函数,但是很好的处理了编码的问题,我回头要试试

2.2 专业数据描述文档
这里学习read_delim进行阅读,有comment的数据集
car_acc<-read_delim("datasets/road-accidents.csv",delim='|',comment="#")

  1. 这个是表达comment,(对啊,在Rmarkdown中确实是使用一个#来进行注释的)

  2. 使用5个来表达标题
    例子略,我还没真正的使用过

3 reprex的使用技巧

reprex存在是为了共享代码??
我以为是为了解决报错之类的,比如安装的包没有及时更新

reprex::reprex(
  ...
)

把要测试代码写入...
之前写过一个例子,把要测试的函数写入reprex中,可以查看当前安装的包的当前版本信息和历史版本信息

大概是这个样子,会生成一个html文档,代码不记得放到哪个地方了

3.2 数据的引入

head(mtcars) %>% deparse()

deparse()解析表的结构,这个时候再复制粘贴就好 clipr::write_clip()执行
[1] "structure(list(mpg = c(21, 21, 22.8, 21.4, 18.7, 18.1), cyl = c(6, "
[2] "6, 4, 6, 8, 6), disp = c(160, 160, 108, 258, 360, 225), hp = c(110, "
[3] "110, 93, 110, 175, 105), drat = c(3.9, 3.9, 3.85, 3.08, 3.15, "
[4] "2.76), wt = c(2.62, 2.875, 2.32, 3.215, 3.44, 3.46), qsec = c(16.46, "
[5] "17.02, 18.61, 19.44, 17.02, 20.22), vs = c(0, 0, 1, 1, 0, 1), "
[6] " am = c(1, 1, 1, 0, 0, 0), gear = c(4, 4, 4, 3, 3, 3), carb = c(4, "
[7] " 4, 1, 1, 2, 1)), row.names = c("Mazda RX4", "Mazda RX4 Wag", "
[8] ""Datsun 710", "Hornet 4 Drive", "Hornet Sportabout", "Valiant""
[9] "), class = "data.frame")"
`{r} head(mtcars) %>% datapasta::tribble_paste() ```tibble::tribble(
~mpg, ~cyl, ~disp, ~hp, ~drat, ~wt, ~qsec, ~vs, ~am, ~gear, ~carb,
21, 6, 160, 110, 3.9, 2.62, 16.46, 0, 1, 4, 4,
21, 6, 160, 110, 3.9, 2.875, 17.02, 0, 1, 4, 4,
22.8, 4, 108, 93, 3.85, 2.32, 18.61, 1, 1, 4, 1,
21.4, 6, 258, 110, 3.08, 3.215, 19.44, 1, 0, 3, 1,
18.7, 8, 360, 175, 3.15, 3.44, 17.02, 0, 0, 3, 2,
18.1, 6, 225, 105, 2.76, 3.46, 20.22, 1, 0, 3, 1
)
datapasta::tribble_paste()直接发生inplace反馈的反馈tibble表格形式

3.3 其他更多的功能

3.3.1 reprex_invert()=the opposite of reprex()
3.3.2 reprex_clean() when you copy/paste from github or stackoverflow
3.3.3 reprex_rescue() when you are dealing with copy/paste from R Console
3.3.4 reprex::reprex(si=TRUE) add session info in a folding style

3.4 指定网站发布(学长这个部分我没太懂,回头可以补一下)

3.4.1 “gh” for Github-Flavored Markdown,the default
3.4.2 "so" for StackOverflow Markdown
3.4.3 "ds" for Discourse e..g.community.rstudio.com

3.5 可复现的例子

需要提前写好代码,想要知道执行后使用者的本地配置
先写好需要的代码

library(dplyr)
mtcars %>% dim()
mtcars %>% summary()

然后复制ctrl+c,再执行代码

reprex::reprex(si=TRUE)

此时会生成一个html文件,参考上图
注意下方有一个session info记录了当前的配置,点击后出现
这样就可以知道当前你的系统信息和相关包的安装情况
这个时候如果你的剪贴版没有被覆盖的话,在github的一个对话框中,执行ctrl+v,会发现html代码。。

4 dplyr

4.1 not: ~!

输出所有非数字型的

msleep %>% 
  select_if(~!is.numeric(.)) %>% 
  glimpse

Observations: 83
Variables: 5
$ name "Cheetah", "Owl monkey", "Mountain beaver", "Greater short-tailed shrew",...
$ genus "Acinonyx", "Aotus", "Aplodontia", "Blarina", "Bos", "Bradypus", "Callorh...
$ vore "carni", "omni", "herbi", "omni", "herbi", "herbi", "carni", NA, "carni",...
$ order "Carnivora", "Primates", "Rodentia", "Soricomorpha", "Artiodactyla", "Pil...
$ conservation "lc", NA, "nt", "lc", "domesticated", NA, "vu", NA, "domesticated", "lc",...

符号表示:~=function()

     :!=not 

4.2 select_*
4.2.1 select_all=rename_all

让名字变为大写
mtcars %>% select_all(toupper) %>% head

让名字变为小写
mtcars %>% select_all(tolower) %>% head

我发现一个问题,我不会编程的原因应该是我不会想问题,我脑子里面没有解决问题的思路,应该多看一些复杂的问题就好了。

Guess you like

Origin www.cnblogs.com/gaowenxingxing/p/11989488.html