R (programming language): Difference between revisions

Content deleted Content added
m CE
CE
Line 309:
 
=== Native pipe operator ===
In R version 4.1.0, a native [[Pipeline (computing)|pipe operator]], <code>|></code>, was introduced.<ref>{{Cite web |title=R: R News |url=https://cran.r-project.org/doc/manuals/r-devel/NEWS.html |access-date=2024-03-14 |website=cran.r-project.org}}</ref> This operator allows users to chain functions together one after another, insteadrather ofthan ausing nested function callcalls.
 
<syntaxhighlight lang="rout">
Line 319:
</syntaxhighlight>
 
Another alternative to nested functions, inis contrastthe touse usingof theintermediate pipe characterobjects, israther usingthan intermediatethe objectspipe operator:
 
<syntaxhighlight lang="rout">
Line 326:
> print(num_mtcars_subset)
[1] 11
</syntaxhighlight>While the pipe operator can produce code that is easier to read, it hasis been advisedadvisable to pipechain together at most 10 to -15 lines andof code using this operator, as well as to chunk code into [[Task (project management)|sub-tasks]] whichthat are saved into objects withhaving meaningful names.<ref>{{Cite book |last=Wickham |first=Hadley |url=https://r4ds.hadley.nz/ |title=R for data science: import, tidy, transform, visualize, and model data |last2=Çetinkaya-Rundel |first2=Mine |last3=Grolemund |first3=Garrett |date=2023 |publisher=O'Reilly |isbn=978-1-4920-9740-2 |edition=2nd |___location=Beijing; Sebastopol, CA |chapter=4 Workflow: code style |oclc=on1390607935 |chapter-url=https://r4ds.hadley.nz/workflow-style.html}}</ref>
 
HereThe following is an example withhaving fewer than 10 lines, thatwhich some readers may stillfind struggledifficult to grasp withoutin the absence of intermediate named steps:<syntaxhighlight lang="r" line="1">(\(x, n = 42, key = c(letters, LETTERS, " ", ":", ")"))
strsplit(x, "")[[1]] |>
(Vectorize(\(chr) which(chr == key) - 1))() |>