Skip to Content
R 语言经典实例(原书第 2 版)
book

R 语言经典实例(原书第 2 版)

by J.D. Long, Paul Teetor
June 2020
Beginner to intermediate
522 pages
9h 6m
Chinese
China Machine Press
Content preview from R 语言经典实例(原书第 2 版)
数据转换
185
于数据框 df,其中包含变量 grouping_var,我们想要将函数 fun 应用于 v1 v2
的所有组合,可以使用 group_by
df %>%
group_by(v1, v2) %>%
summarize(
result_var = fun(value_var)
)
6.6.3 讨论
让我们看一个特定的例子,其中输入数据框 df 包含一个我们想要分组的变量 my_
group,以及一个名为 values 的字段,我们想要计算一些统计信息:
df <- tibble(
my_group = c("A", "B","A", "B","A", "B"),
values = 1:6
)
df %>%
group_by(my_group) %>%
summarize(
avg_values = mean(values),
tot_values = sum(values),
count_values = n()
)
#> # A tibble: 2 x 4
#> my_group avg_values tot_values count_values
#> <chr> <dbl> <int> <int>
#> 1 A 3 9 3
#> 2 B 4 12 3
输出中,根据我们定义的三个汇总字段,每个分组都有一个计算结果。
值得注意的是,summarize 函数将默默地从 group_by 中删除最后一个分组
变量。这是自动完成的,因为将分组保留在适当的位置,将意味着没有任何组
在其中有多行。最后一个分组变量仅从分组向量中删除 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

Aurélien Géron
大规模数据分析和建模:基于 Spark 与 R

大规模数据分析和建模:基于 Spark 与 R

Javier Luraschi, Kevin Kuo, Edgar Ruiz
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey

Publisher Resources

ISBN: 9787111656814