Scripts2026年7月28日·1 分钟阅读

dplyr — A Grammar of Data Manipulation for R

dplyr is an R package that provides a consistent set of verbs (filter, select, mutate, summarise, arrange) for transforming and summarizing tabular data. It is the most popular data wrangling tool in the R ecosystem and a core member of the tidyverse.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
dplyr Guide
直接安装命令
npx -y tokrepo@latest install 0ec34293-8ac7-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

dplyr is an R package by Hadley Wickham that provides a concise, consistent grammar for the most common data manipulation tasks. Instead of remembering different base R functions with different interfaces, dplyr offers five core verbs that cover the vast majority of data transformation needs. It is designed to work with the pipe operator and integrates tightly with the rest of the tidyverse.

What dplyr Does

  • Filters rows based on conditions with filter()
  • Selects, renames, and reorders columns with select() and rename()
  • Creates new columns or transforms existing ones with mutate() and transmute()
  • Aggregates data with summarise() combined with group_by() for split-apply-combine workflows
  • Joins tables with left_join(), inner_join(), anti_join(), and other relational verbs

Architecture Overview

dplyr operates on data frames and tibbles through a verb-based API. Internally, it uses a lazy evaluation system where expressions are captured as quosures and evaluated in the data context. The package can dispatch operations to different backends: in-memory R data frames, database tables via dbplyr (translating dplyr verbs to SQL), or Apache Arrow datasets via arrow. This backend-agnostic design lets the same code work on datasets ranging from kilobytes to terabytes.

Self-Hosting & Configuration

  • Install from CRAN with install.packages("dplyr") or as part of the tidyverse
  • Works with R 3.6+; recommended to use with tibbles for cleaner printing
  • Enable database backends by installing dbplyr and a DBI-compatible driver (RPostgres, RSQLite, etc.)
  • Use .by argument (dplyr 1.1+) for per-operation grouping without persistent group_by()
  • Combine with purrr for row-wise or list-column operations

Key Features

  • Consistent verb-based API makes code readable and composable via the pipe operator
  • Grouped operations with group_by() handle split-apply-combine patterns in a single pipeline
  • Backend-agnostic: the same dplyr code runs on data frames, databases, and Arrow tables
  • Tidy selection helpers (starts_with(), where(), everything()) simplify column selection
  • Window functions (lag, lead, cumsum, row_number, dense_rank) work naturally inside mutate

Comparison with Similar Tools

  • pandas (Python) — similar capabilities but uses method chaining and bracket indexing; dplyr's verb API is often considered more readable
  • data.table (R) — faster for very large in-memory datasets; dplyr trades raw speed for a more approachable syntax
  • SQL — dplyr verbs map closely to SQL clauses; dbplyr translates dplyr pipelines to SQL automatically
  • Polars (Python/Rust) — newer, very fast DataFrame library; dplyr has a larger ecosystem of extensions and educational resources in R
  • base R — subset(), transform(), aggregate() cover similar ground but with inconsistent interfaces; dplyr unifies them

FAQ

Q: Is dplyr fast enough for large datasets? A: dplyr handles millions of rows efficiently. For very large data (100M+ rows), data.table or Arrow backends may be faster. Use dtplyr to get data.table speed with dplyr syntax.

Q: Can dplyr query databases directly? A: Yes. With dbplyr, dplyr verbs are translated to SQL and executed on the database. Call collect() to pull results into R.

Q: What is the difference between dplyr and the tidyverse? A: dplyr is one package focused on data manipulation. The tidyverse is a collection of packages (ggplot2, tidyr, readr, purrr, dplyr, and more) that share a common design philosophy.

Q: How do I apply a function to multiple columns at once? A: Use across() inside mutate() or summarise(): summarise(across(where(is.numeric), mean)).

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产