Skip to Content
R在数据科学中的应用,第2版
book

R在数据科学中的应用,第2版

by Hadley Wickham, Mine Cetinkaya-Rundel, Garrett Grolemund
May 2025
Intermediate to advanced
578 pages
8h 9m
Chinese
O'Reilly Media, Inc.
Content preview from R在数据科学中的应用,第2版

第 12 章 逻辑向量 逻辑向量

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

导言

在本章中,你将学习处理逻辑向量的工具。逻辑向量是最简单的向量类型,因为每个元素只能是三种可能值中的一种:TRUE逻辑向量是最简单的向量类型,因为每个元素只能是以下三种可能值之一: ,FALSE ,和NA 。在原始数据中发现逻辑向量的情况比较少见,但在几乎所有分析过程中都会创建和处理逻辑向量。

首先,我们将讨论创建逻辑矢量的最常用方法:数字比较。然后,您将了解如何使用布尔代数来组合不同的逻辑矢量,以及一些有用的总结。最后,我们将学习 if_else()case_when()这两个有用的函数,它们可以利用逻辑矢量进行有条件的更改。

先决条件

本章将学习的大部分函数都由基本 R 提供,因此我们不需要 tidyverse,但我们仍将加载它,以便使用 mutate(), filter()和朋友来处理数据帧。我们还将继续从 nycflights13::flights数据集中提取示例。

library(tidyverse)
library(nycflights13)

不过,随着我们开始使用更多的工具,并不总能找到完美的真实例子。因此,我们将开始使用 c():

x <- c(1, 2, 3, 5, 7, 11, 13)
x * 2
#> [1]  2  4  6 10 14 22 26

这样就更容易解释单个函数,但代价是更难看出它如何应用于你的数据问题。请记住,我们对自由浮动向量所做的任何操作,都可以用 mutate()等函数对数据帧内的变量进行操作。

df <- tibble(x)
df |> 
  mutate(y = x *  2)
#> # A tibble: 7 × 2
#>       x     y
#>   <dbl> <dbl>
#> 1     1     2
#> 2     2     4
#> 3     3     6
#> 4     5    10
#> 5     7    14
#> 6    11    22
#> # … with 1 more row

比较

创建逻辑向量的常用方法是通过与<,<=,>,>=,!=== 进行数字比较。到目前为止,我们主要是在逻辑变量中临时创建逻辑变量。 filter()中临时创建逻辑变量--它们被计算、使用,然后被丢弃。例如,下面的过滤器可以找到所有大致准时到达的白天出发航班:

flights |> 
  filter(dep_time > 600 & dep_time < 2000 & abs(arr_delay) < 20)
#> # A tibble: 172,286 × 19
#>    year month   day dep_time sched_dep_time dep_delay arr_time sched_arr_time
#>   <int> <int> <int>    <int>          <int>     <dbl>    <int>          <int>
#> 1  2013     1     1      601            600         1      844            850
#> 2  2013     1     1      602            610        -8      812            820
#> 3  2013     1     1      602            605        -3      821            805
#> 4  2013     1     1      606            610        -4      858            910
#> 5  2013     1     1      606            610        -4      837            845
#> 6  2013     1     1      607            607         0      858            915
#> # … with 172,280 more rows, and 11 more variables: arr_delay ...
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

R深度学习权威指南

R深度学习权威指南

Posts & Telecom Press, Joshua F. Wiley
AI工程

AI工程

Chip Huyen
Raku学习手册

Raku学习手册

brian d foy

Publisher Resources

ISBN: 9798341657304