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版

第 14 章 弦乐 字符串

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

导言

到目前为止,你已经使用了大量字符串,但对其中的细节了解不多。现在是时候深入了解字符串,了解字符串的作用,并掌握一些强大的字符串操作工具。

我们将从创建字符串和字符矢量的细节开始。然后,您将深入了解从数据中创建字符串的过程,接着是相反的过程:从数据中提取字符串。然后,我们将讨论处理单个字母的工具。本章最后将介绍处理单个字母的函数,并简要讨论在使用其他语言时,你对英语的期望可能会在哪些方面产生偏差。

在下一章中,我们将继续使用字符串,进一步了解正则表达式的强大功能。

先决条件

在本章中,我们将使用 stringr 包中的函数,它是核心 tidyverse 的一部分。我们还将使用 babynames 数据,因为它提供了一些有趣的字符串操作。

library(tidyverse)
library(babynames)

你可以很快判断出自己在使用 stringr 函数,因为所有 stringr 函数都以str_ 开头。如果你使用 RStudio,这一点尤其有用,因为输入str_ 会触发自动完成功能,让你可以唤起对可用函数的记忆。

str_c typed into the RStudio console with the autocomplete tooltip shown on top, which lists functions beginning with str_c. The function signature and beginning of the main page for the highlighted function from the autocomplete list are shown in a panel to its right.

创建字符串

在本书的前面部分,我们顺便创建了字符串,但没有讨论细节。首先,你可以使用 单引号 (') 或双引号 (") 创建字符串。两者在行为上没有区别,因此为了保持一致,tidyverse 风格指南建议使用" ,除非字符串包含多个"

string1 <- "This is a string"
string2 <- 'If I want to include a "quote" inside a string, I use single quotes'

如果忘记关闭引号,就会看到+ ,继续提示:

> "This is a string without a closing quote
+ 
+ 
+ HELP I'M STUCK IN A STRING

如果您遇到这种情况,又不知道该关闭哪个报价,请按 Escape 取消,然后再试一次。

逃离

要在字符串中包含单引号或双引号,可以使用\ 对其进行 "转义":

double_quote <- "\"" # or '"'
single_quote <- '\'' # or "'"

因此,如果要在字符串中包含字面反斜线,就需要转义:"\\"

backslash <- "\\"

请注意,字符串的打印表示与字符串本身并不相同,因为打印表示会显示转义符(换句话说,打印字符串时,可以复制并粘贴输出来重新创建该字符串)。要查看字符串的原始内容,请使用 str_view():1

x <- c(single_quote, double_quote, backslash)
x
#> [1] "'"  "\"" "\\"

str_view(x)
#> [1] │ '
#> [2] │ "
#> [3] │ \

原始字符串

创建带有多个引号或反斜线的字符串很快就会引起混乱。为了说明这个问题,让我们创建一个字符串,其中包含我们定义double_quotesingle_quote 变量的代码块的内容:

tricky <- 
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