日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

rstudio 管道符号_R中的管道指南

發(fā)布時(shí)間:2023/11/29 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 rstudio 管道符号_R中的管道指南 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

rstudio 管道符號(hào)

R基礎(chǔ)知識(shí) (R Fundamentals)

Data analysis often involves many steps. A typical journey from raw data to results might involve filtering cases, transforming values, summarising data, and then running a statistical test. But how can we link all these steps together, while keeping our code efficient and readable? Enter the pipe, R’s most important operator for data processing.

數(shù)據(jù)分析通常涉及許多步驟。 從原始數(shù)據(jù)到結(jié)果的典型過(guò)程可能涉及篩選案例,轉(zhuǎn)換值,匯總數(shù)據(jù),然后運(yùn)行統(tǒng)計(jì)測(cè)試。 但是,如何在保持代碼高效和可讀性的同時(shí)將所有這些步驟鏈接在一起? 輸入管道,R是數(shù)據(jù)處理中最重要的運(yùn)算符。

管道做什么? (What does the pipe do?)

The pipe operator, written as %>%, has been a longstanding feature of the magrittr package for R. It takes the output of one function and passes it into another function as an argument. This allows us to link a sequence of analysis steps.

管道運(yùn)算符,寫(xiě)為%>% ,是R的magrittr包的一個(gè)長(zhǎng)期功能。它將一個(gè)函數(shù)的輸出傳遞給另一個(gè)函數(shù)作為參數(shù)。 這使我們可以鏈接一系列分析步驟。

To visualise this process, imagine a factory with different machines placed along a conveyor belt. Each machine is a function that performs a stage of our analysis, like filtering or transforming data. The pipe therefore works like a conveyor belt, transporting the output of one machine to another for further processing.

為了可視化此過(guò)程,請(qǐng)?jiān)O(shè)想一家工廠,在傳送帶上放置不同的機(jī)器。 每臺(tái)機(jī)器都是執(zhí)行我們分析階段的功能,例如過(guò)濾或轉(zhuǎn)換數(shù)據(jù)。 因此,管道就像傳送帶一樣工作,將一臺(tái)機(jī)器的輸出輸送到另一臺(tái)機(jī)器進(jìn)行進(jìn)一步處理。

A tasty analysis procedure. Image: Shutterstock美味的分析程序。 圖片:Shutterstock

We can see exactly how this works in a real example using the mtcars dataset. This dataset comes with base R, and contains data about the specs and fuel efficiency of various cars. The code below groups the data by the number of cylinders in each car, and then returns the mean miles-per-gallon of each group. Make sure to install the tidyverse suite of packages before running this code, since it includes both the pipe and the group_by and summarise functions.

我們可以使用mtcars數(shù)據(jù)集在一個(gè)實(shí)際示例中確切地看到它是如何工作的。 該數(shù)據(jù)集帶有基礎(chǔ)R,并包含有關(guān)各種汽車(chē)的規(guī)格和燃油效率的數(shù)據(jù)。 下面的代碼按每輛車(chē)的氣缸數(shù)對(duì)數(shù)據(jù)進(jìn)行分組,然后返回每組的平均每加侖英里數(shù)。 請(qǐng)確保安裝的tidyverse運(yùn)行此代碼之前包的套裝,因?yàn)樗ü艿篮蚲roup_by和summarise的功能。

library(tidyverse)result <- mtcars %>%
group_by(cyl) %>%
summarise(meanMPG = mean(mpg))

The pipe operator feeds the mtcars dataframe into the group_by function, and then the output of group_by into summarise. The outcome of this process is stored in the tibble result, shown below.

管道操作符饋送mtcars數(shù)據(jù)幀到group_by函數(shù),然后輸出group_by成summarise 。 該過(guò)程的結(jié)果存儲(chǔ)在小標(biāo)題result ,如下所示。

Mean miles-per-gallon of vehicles in the mtcars dataset, grouped by number of engine cylinders.mtcars數(shù)據(jù)集中車(chē)輛的平均每加侖英里數(shù),按發(fā)動(dòng)機(jī)氣缸數(shù)分組。

Although this example is very simple, it demonstrates the basic pipe workflow. To go even further, I’d encourage playing around with this. Perhaps swap and add new functions to the ‘pipeline’ to gain more insight into the data. Doing this is the best way to understand how to work with the pipe. But why should we use it in the first place?

盡管此示例非常簡(jiǎn)單,但是它演示了基本的管道工作流程。 為了更進(jìn)一步,我鼓勵(lì)您嘗試一下。 也許交換并向“管道”添加新功能以獲得對(duì)數(shù)據(jù)的更多了解。 這樣做是了解如何使用管道的最佳方法。 但是為什么我們首先要使用它呢?

為什么要使用管道? (Why should we use the pipe?)

The pipe has a huge advantage over any other method of processing data in R: it makes processes easy to read. If we read %>% as “then”, the code from the previous section is very easy to digest as a set of instructions in plain English:

與R中的任何其他數(shù)據(jù)處理方法相比,管道具有巨大的優(yōu)勢(shì):它使過(guò)程易于閱讀。 如果我們將%>%讀為“ then”,那么上一節(jié)中的代碼很容易理解為一組簡(jiǎn)單的英語(yǔ)說(shuō)明:

Load tidyverse packagesTo get our result, take the mtcars dataframe, THEN
Group its entries by number of cylinders, THEN
Compute the mean miles-per-gallon of each group

This is far more readable than if we were to express this process in another way. The two options below are different ways of expressing the previous code, but both are worse for a few reasons.

這比我們用另一種方式來(lái)表達(dá)此過(guò)程更具可讀性。 下面的兩個(gè)選項(xiàng)是表示先前代碼的不同方式,但是由于一些原因,它們都較差。

# Option 1: Store each step in the process sequentially
result <- group_by(mtcars, cyl)
result <- summarise(result, meanMPG = mean(mpg))# Option 2: chain the functions together
> result <- summarise(
group_by(mtcars, cyl),
meanMPG = mean(mpg))

Option 1 gets the job done, but overwriting our output dataframe result in every line is problematic. For one, doing this for a procedure with lots of steps isn’t efficient and creates unnecessary repetition in the code. This repetition also makes it harder to identify exactly what is changing on each line in some cases.

選項(xiàng)1可以完成工作,但是覆蓋每一行的輸出數(shù)據(jù)幀result是有問(wèn)題的。 首先,對(duì)具有很多步驟的過(guò)程執(zhí)行此操作效率不高,并在代碼中造成不必要的重復(fù)。 這種重復(fù)還使得在某些情況下更難于準(zhǔn)確地確定每條線上的變化。

Option 2 is even less practical. Nesting each function we want to use gets ugly fast, especially for long procedures. It’s hard to read, and harder to debug. This approach also makes it tough to see the order of steps in the analysis, which is bad news if you want to add new functionality later.

選項(xiàng)2甚至不那么實(shí)用。 嵌套我們要使用的每個(gè)函數(shù)很快就會(huì)很麻煩,特別是對(duì)于長(zhǎng)過(guò)程。 它很難閱讀,也很難調(diào)試。 這種方法還使得很難查看分析中的步驟順序,如果您以后要添加新功能,則這是個(gè)壞消息。

It’s easy to see how using the pipe can substantially improve most R scripts. It makes analyses more readable, removes repetition, and simplifies the process of adding and modifying code. Is there anything it can’t do?

很容易看到使用管道如何可以大大改善大多數(shù)R腳本。 它使分析更具可讀性,消除重復(fù),并簡(jiǎn)化了添加和修改代碼的過(guò)程。 有什么不能做的嗎?

管道的局限性是什么? (What are the pipe’s limitations?)

Although it’s immensely handy, the pipe isn’t useful in every situation. Here are a few of its limitations:

盡管非常方便,但是管道并不是在每種情況下都有用。 這里有一些限制:

  • Because it chains functions in a linear order, the pipe is less applicable to problems that include multidirectional relationships.

    由于管道按線性順序鏈接功能,因此管道不適用于包含多向關(guān)系的問(wèn)題。
  • The pipe can only transport one object at a time, meaning it’s not so suited to functions that need multiple inputs or produce multiple outputs.

    該管道一次只能傳送一個(gè)對(duì)象,這意味著它不適用于需要多個(gè)輸入或產(chǎn)生多個(gè)輸出的功能。
  • It doesn’t work with functions that use the current environment, nor functions that use lazy evaluation. Hadley Wickham’s book “R for Data Science” has a couple of examples of these.

    它不適用于使用當(dāng)前環(huán)境的函數(shù),也不適用于使用惰性求值的函數(shù)。 哈德利·威克漢姆(Hadley Wickham)的書(shū)“ R for Data Science”(數(shù)據(jù)科學(xué)的R)中有兩個(gè)例子 。

These things are to be expected. Just as you’d struggle to build a house with a single tool, no lone feature will solve all your programming problems. But for what it’s worth, the pipe is still pretty versatile. Although this piece focused on the basics, there’s plenty of scope for using the pipe in advanced or creative ways. I’ve used it in a variety of scripts, data-focused and not, and it’s made my life easier in each instance.

這些事情是可以預(yù)期的。 就像您要用一個(gè)工具建造房屋一樣,沒(méi)有任何一項(xiàng)單獨(dú)的功能可以解決您所有的編程問(wèn)題。 但是,就其價(jià)值而言,管道仍然具有多種用途。 盡管本文著重介紹基礎(chǔ)知識(shí),但仍有許多以高級(jí)或創(chuàng)造性方式使用管道的范圍。 我已經(jīng)在各種腳本中使用了它,而不是關(guān)注數(shù)據(jù)的腳本,這使我的生活在每種情況下都更加輕松。

額外的煙斗技巧! (Bonus pipe tips!)

Thanks for reading this far. As a reward, here are some bonus pipe tips and resources:

感謝您閱讀本文。 作為獎(jiǎng)勵(lì),這些是一些額外的管道技巧和資源:

  • Fed up of awkwardly typing %>%? The slightly easier keyboard shortcut CTRL + SHIFT + M will print a pipe in RStudio!

    受夠了笨拙地輸入%>%嗎? 稍微更簡(jiǎn)單的鍵盤(pán)快捷鍵CTRL + SHIFT + M將在RStudio中打印管道!

  • Need style guidance about how to format pipes? Check out this helpful section from ‘R Style Guide’ by Hadley Wickham.

    需要有關(guān)如何格式化管道的樣式指導(dǎo)? 查閱Hadley Wickham撰寫(xiě)的“ R風(fēng)格指南”中的這一有用部分 。

  • Want to learn a bit more about the history of pipes in R? Check out this blog post from Adolfo álvarez.

    想更多地了解R中管道的歷史嗎? 看看Adolfoálvarez的這篇博客文章 。

The pipe is great. It turns your code into a list of readable instructions and has lots of other practical benefits. So now you know about the pipe, use it, and watch your code turn into a narrative.

管子很棒。 它將您的代碼轉(zhuǎn)換為可讀指令列表,并具有許多其他實(shí)際好處。 因此,現(xiàn)在您知道了管道,使用了管道,然后看著代碼變成了敘述。

翻譯自: https://towardsdatascience.com/an-introduction-to-the-pipe-in-r-823090760d64

rstudio 管道符號(hào)

總結(jié)

以上是生活随笔為你收集整理的rstudio 管道符号_R中的管道指南的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。