首页 > 其他分享 >R中的管道符-magrittr

R中的管道符-magrittr

时间:2022-12-08 11:46:36浏览次数:32  
标签:equivalent 等价 hand magrittr 管道 data side

A Forward-Pipe Operator for R

1.基本用法

x %>% f 等价于 f(x)
x %>% f(y) 等价于 f(x, y)
x %>% f %>% g %>% h  等价于 h(g(f(x)))
----------------------------------
x %>% f(y, .) 等价于 f(y, x)
x %>% f(y, z = .) 等价于 f(y, z = x)

如果数据作为函数的第一个参数的话,是不需要占位符placeholder的,但是如果是第二或者更后面的参数,就需要使用 . 作为占位符。

2.构建函数

f <- . %>% cos %>% sin 
等价为
f <- function(.) sin(cos(.)) 

3.%>%, %$%和%<>%的用法

%>%是最常用的一种前向管道符。

%<>%是表示将变量传入函数,得到的结果返回给原来的变量

iris %<>% na.omit() 等价于 iris<-na.omit(iris)

%$%是使用变量的name来表示该变量下name的数值

df<-data.frame("a"=c(1,2,3), "b"=c(4,5,6))

cor(df$a, df$b) 等价于 df %$% cor(a,b)

Overview

The magrittr package offers a set of operators which make your code more readable by:
magrittr工具箱提供了一系列运算符可以使代码可读性提升。

structuring sequences of data operations left-to-right (as opposed to from the inside and out),通过从左到右代替从内到外,格式化数据操作序列

avoiding nested function calls, 避免函数嵌套

minimizing the need for local variables and function definitions, and 减少局部变量和函数定义的数量

making it easy to add steps anywhere in the sequence of operations. 在操作中便于添加步骤

The operators pipe their left-hand side values forward into expressions that appear on the right-hand side, i.e. one can replace f(x) with x %>% f(), where %>% is the (main) pipe-operator. When coupling several function calls with the pipe-operator, the benefit will become more apparent. Consider this pseudo example:

the_data <-
  read.csv('/path/to/data/file.csv') %>%
  subset(variable_a > x) %>%
  transform(variable_c = variable_a/variable_b) %>%
  head(100)

Four operations are performed to arrive at the desired data set, and they are written in a natural order: the same as the order of execution. Also, no temporary variables are needed. If yet another operation is required, it is straightforward to add to the sequence of operations wherever it may be needed.

If you are new to magrittr, the best place to start is the pipes chapter in R for data science.
Basic piping
x %>% f is equivalent to f(x)
x %>% f(y) is equivalent to f(x, y)
x %>% f %>% g %>% h is equivalent to h(g(f(x)))
Here, “equivalent” is not technically exact: evaluation is non-standard, and the left-hand side is evaluated before passed on to the right-hand side expression. However, in most cases this has no practical implication.

The argument placeholder

x %>% f(y, .) is equivalent to f(y, x)
x %>% f(y, z = .) is equivalent to f(y, z = x)
Re-using the placeholder for attributes
It is straightforward to use the placeholder several times in a right-hand side expression. However, when the placeholder only appears in a nested expressions magrittr will still apply the first-argument rule. The reason is that in most cases this results more clean code.

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

The behavior can be overruled by enclosing the right-hand side in braces:

x %>% {f(y = nrow(.), z = ncol(.))} is equivalent to f(y = nrow(x), z = ncol(x))

Building (unary) functions

Any pipeline starting with the . will return a function which can later be used to apply the pipeline to values. Building functions in magrittr is therefore similar to building other values.

f <- . %>% cos %>% sin 
# is equivalent to 
f <- function(.) sin(cos(.)) 

Pipe with exposition of variables

Many functions accept a data argument, e.g. lm and aggregate, which is very useful in a pipeline where data is first processed and then passed into such a function. There are also functions that do not have a data argument, for which it is useful to expose the variables in the data. This is done with the %$% operator:

iris %>%
  subset(Sepal.Length > mean(Sepal.Length)) %$%
  cor(Sepal.Length, Sepal.Width)
#> [1] 0.3361992

data.frame(z = rnorm(100)) %$%
  ts.plot(z)

标签:equivalent,等价,hand,magrittr,管道,data,side
From: https://www.cnblogs.com/xinkevinzhang/p/16965652.html

相关文章

  • ES中摄取管道详解
    一、什么是摄取管道摄取管道​​Ingestpipelines​​摄取管道主要用来在数据被索引之前对数据执行常见的转换。例如,您可以使用管道来移除字段、从文本中提取值以及丰富数......
  • 第3章 使用中间件管道处理请求(ASP.NET Core in Action, 2nd Edition)
    本章包括什么是中间件使用中间件服务静态文件使用中间件添加功能组合中间件以形成管道使用中间件处理异常和错误在上一章中,您对完整的ASP.NETCore应用程序进行了......
  • 【Linux操作系统】vim编辑器和管道与重定向命令以及U盘备份文件学习详解
    一.TTP是什么终端是一种字符型设备,它有多种类型,通常使用tty来简称各种类型的终端设备。tty是Teletype的缩写。Teletype是最早出现的一种终端设备,是由Teletype公司生产......
  • asp.net core 6 管道构建流程之看一看
    .netcore6已经出来很久了,相关的书也看了一些,源码也看了一些,现在梳理一下我的理解。asp.netcore6注册中间件写法publicstaticvoidMain(string[]args){......
  • 管道" | "符号和xargs及exec的区别
    1.管道符号"|"  管道是一种通信机制,通常用于进程间的通信,它表现出来的形式将前面每一个进程的输出(stdout)直接作为下一个进程的输入  [root@prometheus-serverb......
  • 设计模式: 管道模式(Pipeline Pattern)
    目录基础概念使用场景场景描述定义通用管道上下文定义管道上下文处理器定义业务上下文类型和所需处理器业务上下文处理器-校验处理器-生成模型实例处理器-持久化处理......
  • 基于.NET三维控件的个性化管道软件开发
    1简介管道广泛用于化工、工厂、建筑、市政等方面,关系到国计民生。虽然管道设计软件种类繁多,有的也非常强大(然而也非常昂贵),但也并不能完全满足个性化需要。如何快速开发......
  • vmware workstation使用串中管道与putty交互
    添加串口,使用命令的管道\\.\pipe\com_1先启动vm虚机,然后打开putty需要在vmware虚机的grub中设置console=ttyS0,115200n8......
  • golang的两把利器,协程和管道
    golang的协程相信大家都不陌生,在golang中的使用也很简单,只要加上一个关键字「go」即可,虽然说大家都知道,但是真的在实际使用中又遇到这样那样的问题,坑其实还是挺多的。而网上......
  • 12、go的协程和管道
    协程概念又称微线程,纤程,协程是一种用户态的轻量级线程作用:在执行A函数时候,可以随时中断,去执行B函数,然后中断继续执行A函数(可以自动切换),这一切换过程并不是函数调用(没有调......